本文介绍了一页上有相同的局部视图,如何处理装订?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是MVC的新手.我当前正在添加一个人员页面,该页面包含人员的名字,姓氏和家庭地址,这是一个复杂的对象(包括地址行1,地址行2,状态等),投递地址(与家庭地址相同).

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).

因此,我想创建一个用于显示地址的局部视图,但是当我提交表单时,我无法区分哪个值是家庭住址,因为coz家庭住址和传递地址会在地址栏上显示相同的名称形式.

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
}

我的地址局部视图

@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

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>

}

推荐答案

我在这里发现了一个可能的问题:

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

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

In your AddPerson View, you can use:

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

这篇关于一页上有相同的局部视图,如何处理装订?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:03