我有以下名为 _Categories 的部分视图,位于 ~/Views/Category/_Categories 中:
@model IEnumerable<MyBlogSite.Models.BlogPostCategory>
<ul>
@foreach (var item in Model)
{
<li>@Html.DisplayFor(modelItem => item.Name)</li>
}
</ul>
我在 ~/Views/Blog/Index.cshtml 有以下索引视图:
@model IEnumerable<MyBlogSite.Models.BlogPost>
@{
ViewBag.Title = "Index";
}
@Html.RenderPartial("~/Views/Category/_Categories.cshtml", ---- );
<p>
@Html.ActionLink("New Blog Post", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
...
在上面的破折号 (----) 的空间中,我一直在尝试弄清楚如何传入模型。我无法使用 Model.BlogPostCategory,因为它只接受 Model.BlogPost。根据我在这里看到的几篇文章,我还尝试了带有小写“m”的“model”。
我创造视图模型 for my 部分视图,然后传递其数据。例如:
我的视图模型
public class CompanyCreateViewModel
{
public Company Company { get; set; }
public IList<CompanyContact> CompanyContacts { get; set; }
public IQueryable<ContactType> ContactTypes { get; set; }
}
部分视图
@model Invoice.Model.HelperClasses.CompanyCreateViewModel
---
<div class="editor-field">
@Html.TextBoxFor(model => model.Company.FullName)
@Html.ValidationMessageFor(model => model.Company.FullName)
@Html.TextBoxFor(model => model.Company.ShortName)
@Html.ValidationMessageFor(model => model.Company.ShortName)
@Html.TextBoxFor(model => model.Company.TIN)
@Html.ValidationMessageFor(model => model.Company.TIN)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Company.Address.Country)
@Html.TextBoxFor(model => model.Company.Address.City)
@Html.TextBoxFor(model => model.Company.Address.Street)
</div>
---
并通过调用查看部分视图
@model Invoice.Model.HelperClasses.CompanyViewModel
---
<div id="CompanyCreateModal" class="modal hide fade">
@{Html.RenderPartial("CompanyParts/CompanyCreateModal", new Invoice.Model.HelperClasses.CompanyCreateViewModel() { ContactTypes = Model.ContactTypes });
}
</div>
----
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)