开发者

Same partial view on one page, how to handle binding?

I am still new to MVC. I am currently having a add person page, the page includes person first name, last name and home address which is complex object (include addressline1,addressline2, state etc), delivery address (same to home address).

So I'd like to create a partial view which use to display address, but when I submit the form, I can't distinguish which value is for home address, coz home address and delivery address render the same name on the form.

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
}

My Address Partial View

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

Add Person page

  @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 => m开发者_开发百科odel.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("Back to List", "Index")


I found a likely problem here: two nested model properties of same complex type

In your AddPerson View, you can use:

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

instead of:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜