2011-08-31 65 views
2

在我的网页我现在用的是相同的PartialView 2个实例作为使用相同的局部视图的两个实例页面

<div> 
     @Html.Partial("AddressPartial1", Model.Address1) 
</div> 
<div>           
     @Html.Partial("AddressPartial2", Model.Address2) 
</div> 

我能够得到的唯一ID为我在PartialView各个领域的两个实例上通过传递InstanceName并将其连接到字段名称。现在的问题是当我发布我的页面我没有得到我的观点的模型对象。我可以使用FormCollection读取控件的值,但我正在寻找更好的解决方案,以在同一页上使用PartialView的2个实例。 在此先感谢!

这里是我的代码:

模型(为我的网页):

Private m_Address1 As New AddressModel("Address1") 
    Public Property Address1() As AddressModel 
     Get 
      Return m_Address1 
     End Get 
     Set(ByVal value As AddressModel) 
      m_Address1 = value 
     End Set 
    End Property 

    Private m_Address2 As New AddressModel("Address2") 
    Public Property Address2() As AddressModel 
     Get 
      Return m_Address2 
     End Get 
     Set(ByVal value As AddressModel) 
      m_Address2 = value 
     End Set 
    End Property 

模型PartialView:

Public Class AddressModel 
    Public Sub New() 

    End Sub 

    Public Sub New(ModelInstanceName As String) 
     m_ModelInstanceName = ModelInstanceName 
    End Sub 

    Private m_ModelInstanceName As String 
Private m_StreetAddress1 As String 
    Private m_StreetAddress2 As String 
    Private m_City As String 
    Private m_State As String 
    Private m_ZipCode As String 

Public Property ModelInstanceName As String 
     Get 
      Return m_ModelInstanceName 
     End Get 
     Set(value As String) 
      m_ModelInstanceName = value 
     End Set 
    End Property 

Public Property StreetAddress1() As String 
     Get 
      Return m_StreetAddress1 
     End Get 
     Set(ByVal value As String) 
      If (value IsNot Nothing) Then value = value.Trim() 
      If (String.IsNullOrWhiteSpace(value)) Then value = Nothing 

      m_StreetAddress1 = value 
     End Set 
    End Property 

等特性....

部分查看:

@ModelType AddressModel 
<table style="padding-left: 5px; padding-bottom: 10px; width: 99%"> 
    <tr> 
     <td colspan="4"> 
      <div class="editor-label"> 
       <span class="required">@Html.LabelFor(Function(m) m.StreetAddress1)</span> 
       </div> 
       <div class="editor-field"> 
        @Html.TextBox(Model.ModelInstanceName + "_StreetAddress1", Model.StreetAddress1, New With {.style = "width:390px"}) 
        @Html.ValidationMessageFor(Function(m) m.StreetAddress1) 
       </div> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="4"> 
       <div class="editor-field"> 
        @Html.TextBox(Model.ModelInstanceName + "_StreetAddress2", Model.StreetAddress2, New With {.style = "width:390px"}) 
        @Html.ValidationMessageFor(Function(model) model.StreetAddress2) 
       </div> 
      </td> 
     </tr> 

<tr> 
     <td> 
      <div class="editor-label"> 
       <span class="required">@Html.LabelFor(Function(model) model.ZipCode)</span> 
      </div> 
      <div class="editor-field"> 
       @Html.TextBox(Model.ModelInstanceName + "_ZipCode", Model.ZipCode, New With {.style = "width:60px", .maxlength = "5", .onblur = "zipchange('" + Model.ModelInstanceName + "')"}) 
       @Html.ValidationMessageFor(Function(model) model.ZipCode) 
      </div> 
     </td> 
     <td> 
      <div class="editor-label"> 
       <span class="required">@Html.LabelFor(Function(model) model.City)</span> 
      </div> 
      <div class="editor-field"> 
       @Html.TextBox(Model.ModelInstanceName + "_City", Model.City, New With {.style = "width:130px"}) 
       @Html.ValidationMessageFor(Function(model) model.City) 
      </div> 
     </td> 
    </tr> 
</table> 

等等...

+0

请发布您的控制器POST操作,局部视图和模型的代码。 – counsellorben

+1

您没有使用相同的PartialView。您正在使用AddressPartial1和AddressPartial2是不同的MVC – JAS

+0

oops我的错误..我使用相同的PartialView.i.e。 AddressPartial – MVCBeginner

回答

1

皇家旧的,但我只是碰到了同样的问题。 有一个简单的文章here,它解释了问题和解决方案。

你需要做的是这样的:

@{ Html.Partial("AddressPartial", Model.Address, new ViewDataDictionary() 
    { 
     TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "Address1" } 
    }); } 

@{ Html.Partial("AddressPartial", Model.Address, new ViewDataDictionary() 
    { 
     TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "Address2" } 
    }); } 

这将增加必要的前缀控件ID等等,当你回来后正确绑定。 (假设提供的前缀与您的房产名称匹配)

相关问题