Using 2 instances of same partial view on a page
On my page I am using 2 instances of the same PartialView as
<div>
@Html.Partial("AddressPartial1", Model.Address1)
</div>
<div>
@Html.Partial("AddressPartial2", Model.Address2)
</div>
I am able to get unique id for all my fields in the PartialView for both instances by passing the InstanceName and concatenating it to the field names. Now the issue is when I post my page I am not getting the model object for both my views. I can read the values of the controls using FormCollection but I was looking for a better solution to my scenario of using 2 instances of PartialView on the same page. Thanks in advance!
Here is my code:
Model(For my Page):
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
Model for 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
and other properties....
Partial View:
@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">
开发者_JS百科<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>
and so on...
Real old one but I just ran into the same problem. There is a simple article here which explains the problem and the solution.
What you need to do is something like this:
@{ Html.Partial("AddressPartial", Model.Address, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "Address1" }
}); }
@{ Html.Partial("AddressPartial", Model.Address, new ViewDataDictionary()
{
TemplateInfo = new TemplateInfo() { HtmlFieldPrefix = "Address2" }
}); }
This will add the necessary prefix to control ids, etc and bind correctly when you post back. (assuming the supplied prefixes match your property names)
精彩评论