简体   繁体   English

MVC 4 - 如何将模型数据传递给局部视图?

[英]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.

相关问题 如何在MVC中循环中将模型数据从视图传递到局部视图 - How to pass model data from view to partial view in loop in MVC 如何将模型属性传递给具有其他模型的局部视图? - How do I pass a model property to a partial view with a different model? 如何将IEnumerable模型传递给MVC中的局部视图 - How to pass an IEnumerable model to a partial view in MVC 如果另一个局部视图使用其他模型,如何传递具有特定模型的局部视图? - How do I pass a partial view with a specific model if another partial view is using a different model? 如何将倍数和选择性模型传递到MVC剃刀局部视图中? - How to pass multiples and selective Model into MVC razor Partial View? MVC将模型传递给子局部视图 - MVC pass Model to child partial view 如何获得部分视图以显示数据库中的数据 Html.Partial 在 MVC 5 - How do I get a partial view to display with data from a database, Html.Partial in MVC 5 如何提取ID,从列表中选择正确的视图模型并将其传递到局部视图中? - How do I extract an ID, select the correct view model from a list and pass this inside a partial? 如何将项目数据从.net MVC WebGrid传递到部分视图 - How to pass item data from .net MVC WebGrid to Partial View 如何在C#MVC中为模型分配部分视图数据? - How to assign partial view data to model in c# MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM