2011-08-31 84 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等等,當你回來後正確綁定。 (假設提供的前綴與您的房產名稱匹配)

相關問題