繁体   English   中英

如何在ASP.NET MVC中回发嵌套模型

[英]How to post back a nested model in asp.net mvc

首先,很抱歉,如果这个问题具有误导性,但是我真的不知道如何提出这个问题,因此我将尝试举例说明自己。

如果您可以提出一个更好的标题,我会很乐意更改

我有这些模型:

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

在我的EditorTemplate文件夹中,我有此视图

@ModelType ProcesadoraVizcaya.Category 
<div class="category-edit">
    <div>
        @Html.HiddenFor(Function(model) model.CategoryID)
        <div class="info-area">
            <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>
        <hr />
        </div>
        <div class="tags-area">
                @Html.Partial("EditTags",Model.Tags )
        </div>
    </div>
</div>

如您所见,我正在使用局部视图在每个类别中呈现标签

所以我的局部观点是这样的

@ModelType IEnumerable(Of ProcesadoraVizcaya.Tag)

@Html.EditorForModel()

再次在我的EditorTemplate文件夹中,我有一个这样的视图

@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>

至此,一切进展顺利,负载繁重,没有任何麻烦,分别渲染了我的所有类别和标签。

但是当我回发时使用:

<HttpPost()>
Function EditCategories(Categories As IEnumerable(Of Category)) As ActionResult
     Return View(Categories)
End Function

我得到这个:

在此处输入图片说明

如您所见,Tags没什么。

所以我的问题是这样的,我如何将那些标签返回服务器?

(我还有其他方法可以做到这一点,但我想知道是否可以使用这种方法来做到这一点)

(如果您有C#的答案,请告诉我,我将以此作为工作)

thxs!

如您所见,我正在使用局部视图在每个类别中呈现标签

那是你的问题。 您应该使用编辑器模板:

<div class="tags-area">
    @Html.EditorFor(Function(model) model.Tags)
</div>

然后您应该具有对应的~/Views/Shared/EditorTemplates/Tag.vbhtml模板。 您不需要EditTags.vbhtml部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM