在我的页面上,我使用了与
相同的PartialView的2个实例<div>
@Html.Partial("AddressPartial1", Model.Address1)
</div>
<div>
@Html.Partial("AddressPartial2", Model.Address2)
</div>
通过传递InstanceName并将其连接到字段名称,我可以为PartialView中的所有字段获取两个实例的唯一ID。现在的问题是当我发布我的页面时,我没有得到我的两个视图的模型对象。我可以使用FormCollection读取控件的值,但我正在寻找更好的解决方案,以便在同一页面上使用2个PartialView实例。 提前谢谢!
这是我的代码:
模型(对于我的页面):
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 :(得分: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等,并在发回时正确绑定。 (假设提供的前缀与您的属性名称匹配)