MVC,传递回值从多个局部视图在一个页面 [英] MVC, passing values back from multiple partial views in a page

查看:128
本文介绍了MVC,传递回值从多个局部视图在一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,当我试图从我的网页,其中包含两次相同的局部视图值传递回。
我班definiton是象下面这样:

 公共类账户:IEntity
{    公共小数CurrentBalance {搞定;组; }
    公开名单<&人GT;账户持有{搞定;组; }
    //获得一轮EF4.3包枚举不存在的枚举支持为int
    公众诠释StatusValue {搞定;组; }
    公共AccountStatus状态{{返回(AccountStatus)StatusValue; }集合{StatusValue =(int)的值; }}    公众的DateTime AccountOpenDate {搞定;组; }
    公众的DateTime AccountCloseDate {搞定;组; }
    公众的DateTime AccountSuspensionDate {搞定;组; }
    }

它的人,这是我做了一个局部视图(单身的)的清单。

 <&字段集GT;
    <传奇>与人LT; /传说>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Name)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Name)
        @ Html.ValidationMessageFor(型号=> model.Name)
    < / DIV>
      < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Age)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Age)
        @ Html.ValidationMessageFor(型号=> model.Age)
    < / DIV>
< /字段集>

在该帐户的创建页面我包括我创建如下部分景色2。

 < D​​IV ID =Person1是>
        @ Html.Partial(_ CreateAccountHolder)
    < / DIV>     < D​​IV ID =PERSON2>
        @ Html.Partial(_ CreateAccountHolder)
    < / DIV>

当我在看什么贴后背,它所包含的值(姓名和年龄作为人的属性),我把在页面的表单值,我有让他们的拖车预期:
CurrentBalance=19&Status=Closed&AccountOpenDate=12%2F12%2F2012&Name=mustafa&Age=20&Name=sofia&Age=20&AccountCloseDate=12%2F12%2F2012&AccountSuspensionDate=12%2F12%2F2012

但是,当我看着我的创建方法我控制器上我看到了户口持有人名单空。我试着用不同的签名...
公众的ActionResult创建(账户personalaccount,人[]账户持有人)
公众的ActionResult创建(账户personalaccount,列出账户持有人)

如果我只有人的一个局部视图,并有我的控制器是这样,我可以看到正确绑定Person对象。
公众的ActionResult创建(账户personalaccount,个人账户持有)

任何想法,我要去的地方错了?


解决方案

如果我理解正确的情况下做到这一点是使用编辑器模板,而不是局部的观点的一种方式。我有一个小写了关于他们在这里:

codenodes。字press.com - MVC3编辑模板


  • 如果您还没有解决方案中的一个文件夹EditorTemplates然后创建一个在浏览\\共享文件夹

  • 添加一个新的局部视图,并将其命名一样的,你正在渲染,你的情况的人的模式,所以你会称它为Person.cshtml(我知道的部分意见都应该开始用下划线_,但对它需要一个编辑模板被命名为相同型号)

  • 从你的_CreateAccountHolder局部视图到新Person.cshtml编辑模板粘贴code。

  • 在创建页面正是如此渲染你的账户持有人名单:


 < D​​IV ID =人物>
    @ Html.EditorFor(X => x.AccountHolders)
< / DIV>


如果你需要有各地的每个人单独申报单,那么你可以把它们添加到您的编辑模板。有关编辑模板的好处是,你只需要一个调用模板,即使你有你的列表中的多个Person对象 - 无需环路或类似的模板将自动生成每一个Person对象什么。它还名外地正确的,所以应该回来后像这样例如,如果你有你的收藏2人对象:


 账户持有[0] .Name点
账户持有[0]。年龄
账户持有人[1] .Name点
账户持有人[1]。年龄


这里的code为你编辑模板:

  @model人<&字段集GT;
    <传奇>与人LT; /传说>    < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Name)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Name)
        @ Html.ValidationMessageFor(型号=> model.Name)
    < / DIV>
      < D​​IV CLASS =编辑标记>
        @ Html.LabelFor(型号=> model.Age)
    < / DIV>
    < D​​IV CLASS =主编场>
        @ Html.EditorFor(型号=> model.Age)
        @ Html.ValidationMessageFor(型号=> model.Age)
    < / DIV>
< /字段集>

I have problem when I am trying to pass values back from my page which contains the same partial view twice. My class definiton is like below:

public class Account : IEntity
{

    public decimal CurrentBalance { get; set; }
    public List<Person> AccountHolders { get; set; }
    //to get round the non-existing enum support in EF4.3 wrap enum to int
    public int StatusValue { get; set; }
    public AccountStatus Status { get { return (AccountStatus)StatusValue; } set { StatusValue = (int) value; } }

    public DateTime AccountOpenDate { get; set; }
    public DateTime AccountCloseDate { get; set; }
    public DateTime AccountSuspensionDate { get; set; }
    }

It has a List of Person , which I made a partial view for (for a single one).

<fieldset>
    <legend>Person</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
      <div class="editor-label">
        @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Age)
        @Html.ValidationMessageFor(model => model.Age)
    </div>
</fieldset>

In the Create page for the Account I include 2 of the partial views I created as below.

 <div id="Person1">
        @Html.Partial("_CreateAccountHolder" )
    </div>

     <div id="Person2">
        @Html.Partial("_CreateAccountHolder")
    </div>

When I look at what is posted back, it contains the values (Name and Age as the properties of Person) I put in the form values of the page and I have have the tow of them as expected: CurrentBalance=19&Status=Closed&AccountOpenDate=12%2F12%2F2012&Name=mustafa&Age=20&Name=sofia&Age=20&AccountCloseDate=12%2F12%2F2012&AccountSuspensionDate=12%2F12%2F2012

But when I look at my create method on my controller I see the AccountHolder list as null. I tried with various signatures... public ActionResult Create(Account personalaccount, Person [] accountHolders) public ActionResult Create(Account personalaccount, List accountHolders)

If I only have one partial view of Person and have my controller like this, I can see the Person object bound correctly. public ActionResult Create(Account personalaccount, Person accountHolder)

Any ideas as to where I am going wrong?

解决方案

If I understand your scenario correctly one way to accomplish this is to use Editor Templates instead of partial views. I have a little write up about them here:

codenodes.wordpress.com - MVC3 Editor Templates

  • if you don't already have an EditorTemplates folder in your solution then create one in the Views\Shared folder
  • add a new partial view and name it the same as the model you are rendering, in your case Person, so you would call it Person.cshtml (I know partial views are supposed to start with an underscore "_" but for an Editor Template it needs to be named the same as the model)
  • paste the code from your "_CreateAccountHolder" partial view into the new Person.cshtml Editor Template.
  • in your Create page render your AccountHolders list thusly:

<div id="People">
    @Html.EditorFor(x => x.AccountHolders)
</div>

If you need to have individual divs around each Person then you can add these to your Editor Template. The good thing about Editor Templates is that you only need a single call to the template even if you have multiple Person objects in your list - no need to loop or anything like that the template will automatically render each Person object. It also names the field correctly so it should post back something like this if for example you have 2 Person objects in your collection:

AccountHolders[0].Name
AccountHolders[0].Age
AccountHolders[1].Name
AccountHolders[1].Age

Here's the code for you Editor Template:

@model Person

<fieldset>
    <legend>Person</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
      <div class="editor-label">
        @Html.LabelFor(model => model.Age)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Age)
        @Html.ValidationMessageFor(model => model.Age)
    </div>
</fieldset>

这篇关于MVC,传递回值从多个局部视图在一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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