MVC 4 - 我该怎么办模型数据传递到局部视图? [英] MVC 4 - how do I pass model data to a partial view?

查看:200
本文介绍了MVC 4 - 我该怎么办模型数据传递到局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设,将有若干部分,涉及到一个特定的模式(租客)个人资料页 - AboutMe,我的preferences - 这种事情。这些部分的每一个都将是一个局部视图,允许使用AJAX部分页面更新。

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.

当我在TenantController点击一个的ActionResult 我能够创建一个强类型的视图和模型的数据传递给视图的罚款。我不能局部视图实现这一目标。

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. I can't achieve this with partial views.

我创建了一个局部视图 _TenantDetailsPartial

I've created a partial view _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>

然后我有一个现场会提到的部分景色景色我的资料

@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>

如果我换行DIV中的code在 _TenantDetailsPartial @if(型号!= NULL){} 没有东西显示在页面上,所以我猜有被传递到视图空模型。

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.

为什么当我创建从的ActionResult 用户在被传递到视图中的会话一个强类型的看法?在会话用户如何传递到没有从创建局部视图的的ActionResult ?如果我失去了一些东西有关的概念,请解释。

How come when I create a strongly typed view from an ActionResult the 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.

推荐答案

您实际上并不传递模式向部分,你传递一个新的ViewDataDictionary&LT; LetLord.Models.Tenant&GT;( )。试试这个:

You're not actually passing the model to the Partial, you're passing a 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>

这篇关于MVC 4 - 我该怎么办模型数据传递到局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆