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>
等等...
请发布您的控制器POST操作,局部视图和模型的代码。 – counsellorben
您没有使用相同的PartialView。您正在使用AddressPartial1和AddressPartial2是不同的MVC – JAS
oops我的错误..我使用相同的PartialView.i.e。 AddressPartial – MVCBeginner