Basically, here is my code inside the view, and I don't want to repeat instances of WebGrid.
My model
public class UsersWithDetailsModels
{
public IEnumerable<ApplicationUser> ApplicationUsers { get; set; }
public IEnumerable<PersonalInfo> PersonalInfos { get; set; }
public IEnumerable<CreditHistory> CreditHistories { get; set; }
public IEnumerable<Transaction> Transactions { get; set; }
}
My controller
[AllowAnonymous]
public ActionResult Index(UsersWithDetailsModels model)
{
return View(model);
}
Index.cshtml
@model IEnumerable<Lml.Models.UsersWithDetailsModels>
@{
ViewBag.Title = "Home";
Layout = null;
var pg = new WebGrid(Model, defaultSort: "Id", canPage: true, ajaxUpdateContainerId: "borrowersAppliedTable");
pg.Pager(WebGridPagerModes.All);
var hg = new WebGrid(Model, defaultSort: "Id", canPage: true, ajaxUpdateContainerId: "borrowersAppliedAddressTable");
hg.Pager(WebGridPagerModes.All);
var hg = new WebGrid(Model, defaultSort: "Id", canPage: true, ajaxUpdateContainerId: "borrowersAppliedMailingTable");
hg.Pager(WebGridPagerModes.All);
}
Obviously, you can see how redundant this is.
In addition, I have column styles
<!-- TABLE 1 -->
<div class="col-lg-6">
<h2>Borrowers Applied</h2>
<div class="table-responsive">
@pg.GetHtml(
htmlAttributes: new { id = "borrowersAppliedTable" },
tableStyle: "table table-bordered table-hover table-striped",
footerStyle: "table-footer text-center",
columns:
pg.Columns(
pg.Column(columnName: "Id", header: "Entry ID", format: (item) => item.GetSelectLink(item.Id.ToString())),
pg.Column(columnName: "DateApplied", header: "Entry Date"),
pg.Column(columnName: "FullName", header: "Full Name"),
pg.Column(columnName: "Email", header: "Email address")
)
)
</div>
</div>
<!-- TABLE 2 -->
<div class="col-lg-6">
<h2>Home Addresses</h2>
<div class="table-responsive">
@hg.GetHtml(
htmlAttributes: new { id = "borrowersAppliedAddressTable" },
tableStyle: "table table-bordered table-hover table-striped",
footerStyle: "table-footer text-center",
columns:
hg.Columns(
hg.Column(columnName: "Id", header: "Entry ID", format: (item) => item.GetSelectLink(item.Id.ToString())),
hg.Column(columnName: "Street", header: "Street Address"),
hg.Column(columnName: "City", header: "City/Municipality"),
hg.Column(columnName: "Zip", header: "ZIP Code"),
hg.Column(columnName: "State", header: "Province/State"),
hg.Column(columnName: "Country")
))
</div>
</div>
<!-- TABLE 3 -->
<div class="col-lg-6">
<h2>Mailing Addresses</h2>
<div class="table-responsive">
@hg.GetHtml(
htmlAttributes: new { id = "borrowersAppliedMailingTable" },
tableStyle: "table table-bordered table-hover table-striped",
footerStyle: "table-footer text-center",
columns:
hg.Columns(
hg.Column(columnName: "Id", header: "Entry ID", format: (item) => item.GetSelectLink(item.Id.ToString())),
hg.Column(columnName: "MailStreet", header: "Street Address"),
hg.Column(columnName: "MailCity", header: "City/Municipality"),
hg.Column(columnName: "MailZip", header: "ZIP Code"),
hg.Column(columnName: "MailState", header: "Province/State"),
hg.Column(columnName: "MailCountry", header: "Country")
)
)
</div>
</div>
As you can see these rows, I want to apply this to each table.
tableStyle: "table table-bordered table-hover table-striped",
footerStyle: "table-footer text-center",
I do not want to repeat it again and again.
Can someone help me find out the shortcut or is there some short and neat method when displaying multiple WebGrids on the same page?