一页上相同的部分视图,如何处理绑定?

发布于 2024-11-10 17:10:36 字数 3362 浏览 0 评论 0原文

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

所以我想创建一个用于显示地址的部分视图,但是当我提交表单时,我无法区分哪个值是家庭地址,因为家庭地址和送货地址在表单上呈现相同的名称。

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("Back to List", "Index")

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 => 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("Back to List", "Index")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2024-11-17 17:10:36

我在这里发现了一个可能的问题:相同复杂类型的两个嵌套模型属性< /a>

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文