[英]MVC 4 - how do I pass model data to a partial view?
I'm building a profile page that will have a number of sections that relate to a particular model (Tenant) - AboutMe, MyPreferences - those kind of things.我正在构建一个个人资料页面,该页面将包含许多与特定模型(租户)相关的部分 - AboutMe、MyPreferences - 诸如此类。 Each one of those sections is going to be a partial view, to allow for partial page updates using AJAX.
这些部分中的每一个都将是一个局部视图,以允许使用 AJAX 进行局部页面更新。
When I click on an ActionResult
in the TenantController I'm able to create a strongly typed view and the model data is passed to the view fine.当我单击 TenantController 中的
ActionResult
时,我能够创建一个强类型视图,并将模型数据传递给视图。 I can't achieve this with partial views.我无法通过部分视图实现这一点。
I've created a partial view _TenantDetailsPartial
:我创建了一个局部视图
_TenantDetailsPartial
:
@model LetLord.Models.Tenant
<div class="row-fluid">
@Html.LabelFor(x => x.UserName) // this displays UserName when not in IF
@Html.DisplayFor(x => x.UserName) // this displays nothing
</div>
I then have a view MyProfile
that will render mentioned partial views:然后我有一个视图
MyProfile
将呈现提到的部分视图:
@model LetLord.Models.Tenant
<div class="row-fluid">
<div class="span4 well-border">
@Html.Partial("~/Views/Tenants/_TenantDetailsPartial.cshtml",
new ViewDataDictionary<LetLord.Models.Tenant>())
</div>
</div>
If I wrap the code inside the DIV in _TenantDetailsPartial
inside @if(model != null){}
nothing gets displayed on the page, so I'm guessing there is an empty model being passed to the view.如果我将代码包装在 DIV 中的
_TenantDetailsPartial
@if(model != null){}
页面上不会显示任何内容,所以我猜有一个空模型被传递给视图。
How come when I create a strongly typed view from an ActionResult
the user in the 'session' gets passed to the view?当我从
ActionResult
创建强类型视图时,“会话”中的用户如何传递给视图? How can pass the user in the 'session' to a partial view that is not created from an ActionResult
?如何将“会话”中的用户传递到不是从
ActionResult
创建的部分视图? If I'm missing something about the concept, please explain.如果我遗漏了一些关于这个概念的东西,请解释一下。
You're not actually passing the model to the Partial, you're passing a new ViewDataDictionary<LetLord.Models.Tenant>()
.您实际上并没有将模型传递给 Partial,而是传递了一个
new ViewDataDictionary<LetLord.Models.Tenant>()
。 Try this:试试这个:
@model LetLord.Models.Tenant
<div class="row-fluid">
<div class="span4 well-border">
@Html.Partial("~/Views/Tenants/_TenantDetailsPartial.cshtml", Model)
</div>
</div>
Also, this could make it works:此外,这可以使它起作用:
@{
Html.RenderPartial("your view", your_model, ViewData);
}
or要么
@{
Html.RenderPartial("your view", your_model);
}
For more information on RenderPartial and similar HTML helpers in MVC see this popular StackOverflow thread有关 MVC 中 RenderPartial 和类似 HTML 助手的更多信息,请参阅此流行的 StackOverflow 线程
Three ways to pass model data to partial view (there may be more)将模型数据传递给局部视图的三种方式(可能还有更多)
This is view page这是查看页面
Method One Populate at view方法一在视图中填充
@{
PartialViewTestSOl.Models.CountryModel ctry1 = new PartialViewTestSOl.Models.CountryModel();
ctry1.CountryName="India";
ctry1.ID=1;
PartialViewTestSOl.Models.CountryModel ctry2 = new PartialViewTestSOl.Models.CountryModel();
ctry2.CountryName="Africa";
ctry2.ID=2;
List<PartialViewTestSOl.Models.CountryModel> CountryList = new List<PartialViewTestSOl.Models.CountryModel>();
CountryList.Add(ctry1);
CountryList.Add(ctry2);
}
@{
Html.RenderPartial("~/Views/PartialViewTest.cshtml",CountryList );
}
Method Two Pass Through ViewBag方法二通过ViewBag
@{
var country = (List<PartialViewTestSOl.Models.CountryModel>)ViewBag.CountryList;
Html.RenderPartial("~/Views/PartialViewTest.cshtml",country );
}
Method Three pass through model方法三通过模型
@{
Html.RenderPartial("~/Views/PartialViewTest.cshtml",Model.country );
}
I know question is specific to MVC4.我知道问题特定于 MVC4。 But since we are way past MVC4 and if anyone looking for ASP.NET Core , you can use:
但是由于我们已经超越了 MVC4 并且如果有人在寻找ASP.NET Core ,您可以使用:
<partial name="_My_Partial" model="Model.MyInfo" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.