我在 mvc3 中创建了一个 Web 应用程序并创建了两个部分视图,一个具有控件两个下拉列表。第二个有 webgrid,它显示来自数据库的数据。
partialview1.cshtml
@model Mapping.Models.SecurityIdentifierMapping
@using (Html.BeginForm("Mapping", "Home"))
{
@Html.DropDownList("SecurityID", Model.PricingSecurityID, "-- Select SecurityID --")
<br />
@Html.DropDownList("CUSIPID", Model.PricingSecurityID, "-- Select CUSIPID --")
<br />
<button type="submit">Map</button>
}
partialview2.cshtml
@model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
@{
ViewBag.Title = "Mapping";
WebGrid grid = null;
if (Model.Count() > 0 ){
grid = new WebGrid(source: Model,
defaultSort: "Id",
canPage: true,
canSort: true,
rowsPerPage:20);
}
}
<h2>Mapping</h2>
@if (grid != null)
{
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("", header: null, format: @<text>@Html.ActionLink("Edit", "Edit", new { id = (int)item.id }) @Html.ActionLink("Delete", "Delete", new { id = (int)item.id })</text>),
grid.Column("PricingSecurityID"),
grid.Column("CUSIP")
)
)
}
<br />
<p>
@Html.ActionLink("Back", "Index")
</p>
in index.cshtml
<div>
@Html.Partial("_ControlsPartial",)
</div>
<div>
@Html.Partial("_WebGridPartial")
</div>
inside Indexcontroller.cs in Index()
public ActionResult Index()
{
//FOR POPULATE DROPDOWN
//SecurityIdentifierMapping objModel = new SecurityIdentifierMapping();
//objModel.PricingSecurityID = objRepository.GetPricingSecurityID();
//objModel.CUSIP = objRepository.GetCUSIP();
//return View(objModel);
//FOR DISPLAY DATA IN WEBGRID
return View(dbContext.SecurityIdentifierMappings);
}
这里的问题是webgrid partial view
有@model IEnumerable<Mapping.Models.SecurityIdentifierMapping>
并且
controlpartilview
正在有@model Mapping.Models.SecurityIdentifierMapping
那么我如何创建一个 VIEWMODEL.cs 一个新的类,它将有两个模型,我如何在 INDEX(() 中写入它,以便该方法也将填充下拉菜单并在 WEBGRID 中显示数据?