同一个局部视图在一个页面上,如何处理绑定?

时间:2011-06-02 01:34:36

标签: asp.net-mvc-3 model-binding

我还是MVC的新手。我目前有一个添加人页面,页面包括人名,姓和家庭地址,这是复杂的对象(包括地址线1,地址线2,州等),送货地址(与家庭地址相同)。

所以我想创建一个用于显示地址的局部视图,但是当我提交表单时,我无法区分哪个值适用于家庭地址,coz家庭地址和传递地址呈现相同的名称形成。

MyClass的

 Public class Person
    {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public int DateOfBirthMonth { get; set; }
    public Gender Sex { get; set; }

    public AddressModel HomeAddress { get; set; }
    public AddressModel DeliveryAddress { get; set; }

    public Person()
    {
        HomeAddress = new AddressModel();
        DeliveryAddress = new AddressModel();
    }
}

    public class AddressModel
    {
      public string Addr1 { get; set; }
      public string Addr2 { get; set; }
      public string Suburb { get; set; }
   }

public enum Gender
{
    Male = 1,
    Female = 2
}

我的地址部分视图

@model MvcApplication1.Models.AddressModel

<fieldset>
<legend>Address</legend>

<div class="editor-label">
    @Html.LabelFor(model => model.Addr1)
</div>
<div class="editor-field">

    @Html.TextBoxFor(model => model.Addr1)
    @Html.ValidationMessageFor(model => model.Addr1)
</div>

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

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

添加个人页面

  @using MvcApplication1.Models
  @model MvcApplication1.Models.Person

 @{
    ViewBag.Title = "AddPerson";
  }

addPerson的

   
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Person</legend>

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

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


    <div class="editor-label">
        @Html.LabelFor(model => model.Gender)
    </div>        
    <div class="editor-field">
        @Html.DropDownListFor(m => m.Gender, Model.Sex.ToSelectList())
        @Html.ValidationMessageFor(model => model.Sex)
    </div>        


    <div class="editor-field">           
        @{Html.RenderPartial("_Address", Model.HomeAddress);}
    </div> 
    <div class="editor-field">           
        @{Html.RenderPartial("_Address", Model.DeliveryAddress);}
    </div> 
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

        @ Html.ActionLink(“返回列表”,“索引”)

1 个答案:

答案 0 :(得分:2)

我在这里发现了一个可能的问题:two nested model properties of same complex type

在您的AddPerson视图中,您可以使用:

   @{Html.EditorFor(model => model.HomeAddress, "_Address");}
   @{Html.EditorFor(model => model.DeliveryAddress, "_Address");}

而不是:

<div class="editor-field">           
    @{Html.RenderPartial("_Address", Model.HomeAddress);}
</div> 
<div class="editor-field">           
    @{Html.RenderPartial("_Address", Model.DeliveryAddress);}
</div> 
<p>
    <input type="submit" value="Create" />
</p>