C# 如何在asp.net mvc中回发嵌套模型
首先,如果这个问题有误导性,我很抱歉,但我真的不知道如何提问,所以我会尝试用例子来解释我自己 如果你能为这个问题建议一个更好的标题,我将很乐意更改它 我有这些型号:C# 如何在asp.net mvc中回发嵌套模型,c#,asp.net-mvc,vb.net,asp.net-mvc-4,razor-2,C#,Asp.net Mvc,Vb.net,Asp.net Mvc 4,Razor 2,首先,如果这个问题有误导性,我很抱歉,但我真的不知道如何提问,所以我会尝试用例子来解释我自己 如果你能为这个问题建议一个更好的标题,我将很乐意更改它 我有这些型号: Public Class Tag Property TagID As Integer Property Name As String <Column(TypeName:="image")> Property Image As Byte() Property ImageMimeTyp
Public Class Tag
Property TagID As Integer
Property Name As String
<Column(TypeName:="image")>
Property Image As Byte()
Property ImageMimeType As String
Property CategoryID As Integer
Overridable Property Category As Category
End Class
Public Class Category
Property CategoryID As Integer
Property Name As String
Overridable Property Tags As ICollection(Of Tag)
End Class
Function EditCategories() As ActionResult
Dim categories As IEnumerable(Of Category) = UW.CategoryRepository.GetAll
Return View(categories)
End Function
@modeltype IEnumerable(Of Category )
@Using Html.BeginForm("EditCategories", "Admin", FormMethod.Post, New With {.enctype = "multipart/form-data"})
@Html.ValidationSummary(True)
@<fieldset>
<legend>Product</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
<HttpPost()>
Function EditCategories(Categories As IEnumerable(Of Category)) As ActionResult
Return View(Categories)
End Function
现在是我开始把事情复杂化的时候(至少对我来说)
我的观点是这样的:
Public Class Tag
Property TagID As Integer
Property Name As String
<Column(TypeName:="image")>
Property Image As Byte()
Property ImageMimeType As String
Property CategoryID As Integer
Overridable Property Category As Category
End Class
Public Class Category
Property CategoryID As Integer
Property Name As String
Overridable Property Tags As ICollection(Of Tag)
End Class
Function EditCategories() As ActionResult
Dim categories As IEnumerable(Of Category) = UW.CategoryRepository.GetAll
Return View(categories)
End Function
@modeltype IEnumerable(Of Category )
@Using Html.BeginForm("EditCategories", "Admin", FormMethod.Post, New With {.enctype = "multipart/form-data"})
@Html.ValidationSummary(True)
@<fieldset>
<legend>Product</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
<HttpPost()>
Function EditCategories(Categories As IEnumerable(Of Category)) As ActionResult
Return View(Categories)
End Function
在我的EditorTemplate文件夹中,我有一个类似这样的视图
@ModelType IEnumerable(Of ProcesadoraVizcaya.Tag)
@Html.EditorForModel()
@ModelType ProcesadoraVizcaya.Tag
<div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Name)
@Html.ValidationMessageFor(Function(model) model.Name)
</div>
<div>
</div>
</div>
@ModelType ProcesadoraVizcaya.Tag
@LabelFor(函数(模型)model.Name)
@EditorFor(函数(模型)model.Name)
@Html.ValidationMessageFor(函数(模型)model.Name)
到目前为止,一切都进行得很顺利,lazzy load运行时没有任何问题,可以分别呈现我的所有类别和标记
但是当我使用:
Public Class Tag
Property TagID As Integer
Property Name As String
<Column(TypeName:="image")>
Property Image As Byte()
Property ImageMimeType As String
Property CategoryID As Integer
Overridable Property Category As Category
End Class
Public Class Category
Property CategoryID As Integer
Property Name As String
Overridable Property Tags As ICollection(Of Tag)
End Class
Function EditCategories() As ActionResult
Dim categories As IEnumerable(Of Category) = UW.CategoryRepository.GetAll
Return View(categories)
End Function
@modeltype IEnumerable(Of Category )
@Using Html.BeginForm("EditCategories", "Admin", FormMethod.Post, New With {.enctype = "multipart/form-data"})
@Html.ValidationSummary(True)
@<fieldset>
<legend>Product</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
End Using
<HttpPost()>
Function EditCategories(Categories As IEnumerable(Of Category)) As ActionResult
Return View(Categories)
End Function
函数EditCategories(类别为IEnumerable(属于类别))作为ActionResult
返回视图(类别)
端函数
我明白了:
正如你所看到的,标签什么都不是
我的问题是,如何将这些标签返回到服务器
(我有其他方法可以做到这一点,但我想知道是否可以使用这种方法做到这一点)
(如果你有C#中的答案,请让我知道我将从此开始工作)
谢谢
如您所见,我使用局部视图渲染每个类别内的标记
那是你的问题。您应该使用编辑器模板:
<div class="tags-area">
@Html.EditorFor(Function(model) model.Tags)
</div>
@EditorFor(函数(模型)model.Tags)
然后应该有相应的~/Views/Shared/EditorTemplates/Tag.vbhtml
模板。您不需要EditTags.vbhtml
partial
如您所见,我使用局部视图渲染每个类别内的标记
那是你的问题。您应该使用编辑器模板:
<div class="tags-area">
@Html.EditorFor(Function(model) model.Tags)
</div>
@EditorFor(函数(模型)model.Tags)
然后应该有相应的
~/Views/Shared/EditorTemplates/Tag.vbhtml
模板。您不需要EditTags.vbhtml
partial.是的,史蒂文,我的控制器中有它:)是的,史蒂文,我的控制器中有它:)