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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15148103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:15:25  来源:igfitidea点击:

MVC 4 - how do I pass model data to a partial view?

c#asp.net-mvcmodelpartial-viewsasp.net-mvc-views

提问by MattSull

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. Each one of those sections is going to be a partial view, to allow for partial page updates using AJAX.

我正在构建一个个人资料页面,该页面将包含许多与特定模型(租户)相关的部分 - AboutMe、MyPreferences - 诸如此类。这些部分中的每一个都将是一个局部视图,以允许使用 AJAX 进行局部页面更新。

When I click on an ActionResultin the TenantController I'm able to create a strongly typed view and the model data is passed to the view fine. I can't achieve this with partial views.

当我单击ActionResultTenantController 中的 an 时,我能够创建一个强类型视图,并将模型数据传递给视图。我无法通过部分视图实现这一点。

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 MyProfilethat 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 _TenantDetailsPartialinside @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 ActionResultthe user in the 'session' gets passed to the view? How can pass the user in the 'session' to a partial view that is not created from an ActionResult? If I'm missing something about the concept, please explain.

当我从ActionResult“会话”中的用户创建强类型视图时,如何传递给视图?如何将“会话”中的用户传递到不是从ActionResult. 如果我遗漏了一些关于这个概念的东西,请解释一下。

采纳答案by Daniel Imms

You're not actually passing the model to the Partial, you're passing a new ViewDataDictionary<LetLord.Models.Tenant>(). Try this:

您实际上并没有将模型传递给 Partial,而是传递了一个new ViewDataDictionary<LetLord.Models.Tenant>(). 尝试这个:

@model LetLord.Models.Tenant
<div class="row-fluid">
    <div class="span4 well-border">
         @Html.Partial("~/Views/Tenants/_TenantDetailsPartial.cshtml", Model)
    </div>
</div>

回答by lzlstyle

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 线程

回答by Arun Prasad E S

Three ways to pass model data to partial view (there may be more)

将模型数据传递给局部视图的三种方式(可能还有更多)

This is view page

这是查看页面

Method OnePopulate 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 TwoPass Through ViewBag

方法二通过ViewBag

@{
    var country = (List<PartialViewTestSOl.Models.CountryModel>)ViewBag.CountryList;
    Html.RenderPartial("~/Views/PartialViewTest.cshtml",country );
}

Method Threepass through model

方法三通过模型

@{
    Html.RenderPartial("~/Views/PartialViewTest.cshtml",Model.country );
}

enter image description here

在此处输入图片说明

回答by Irshu

I know question is specific to MVC4. But since we are way past MVC4 and if anyone looking for ASP.NET Core, you can use:

我知道问题特定于 MVC4。但是由于我们已经超越了 MVC4 并且如果有人在寻找ASP.NET Core,您可以使用:

<partial name="_My_Partial" model="Model.MyInfo" />