【问题标题】:How to post back a nested model in asp.net mvc如何在 asp.net mvc 中回发嵌套模型
【发布时间】:2013-01-27 13:24:40
【问题描述】:

首先很抱歉,如果这个问题具有误导性,但我真的不知道如何提出这个问题,所以我会尝试用例子来解释自己。

如果您可以为问题建议一个更好的标题,我很乐意更改它

我有这些模型:

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>

如您所见,我使用局部视图在每个 Categories

中呈现 Tags

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

@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

我明白了:

正如你所见,标签什么都不是。

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

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

(如果你有 C# 中的答案,请告诉我,我会从那里开始工作)

谢谢!

【问题讨论】:

  • 是的,史蒂文,我的控制器里有它 :)

标签: c# asp.net-mvc vb.net asp.net-mvc-4 razor-2


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2011-03-09
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多