简体   繁体   English

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

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

First of all sorry if the question is misleading but I don't really get how to ask the question so I will try to explain myself with examples. 首先,很抱歉,如果这个问题具有误导性,但是我真的不知道如何提出这个问题,因此我将尝试举例说明自己。

If you can suggest a better title for the question I will gladly change it 如果您可以提出一个更好的标题,我会很乐意更改

I have these model: 我有这些模型:

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

Then my controller is like this: 然后我的控制器是这样的:

Function EditCategories() As ActionResult
        Dim categories As IEnumerable(Of Category) = UW.CategoryRepository.GetAll
        Return View(categories)
End Function

Now is when I start complicate things (at least for me) 现在是我开始使事情复杂化的时候(至少对我来说)

My view is like this: 我的看法是这样的:

@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

in my EditorTemplate folder I have this view 在我的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>

as you can see I'm using a partial view to render Tags inside each Categories 如您所见,我正在使用局部视图在每个类别中呈现标签

so my partial view is like this 所以我的局部观点是这样的

@ModelType IEnumerable(Of ProcesadoraVizcaya.Tag)

@Html.EditorForModel()

again in my EditorTemplate folder I have a view like this 再次在我的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>

To this point everything goes well and lazzy load runs without any trouble rendering all my categories and tags respectively. 至此,一切进展顺利,负载繁重,没有任何麻烦,分别渲染了我的所有类别和标签。

but when I post back using: 但是当我回发时使用:

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

I get this: 我得到这个:

在此处输入图片说明

As you can see Tags is nothing. 如您所见,Tags没什么。

So my question is like this how do I return to the server those tags? 所以我的问题是这样的,我如何将那些标签返回服务器?

(I have others methods to do that but I will like to know if is possible to do it using this approach) (我还有其他方法可以做到这一点,但我想知道是否可以使用这种方法来做到这一点)

(if you have the answer in C# please let me know I will work from that) (如果您有C#的答案,请告诉我,我将以此作为工作)

thxs! thxs!

as you can see I'm using a partial view to render Tags inside each Categories 如您所见,我正在使用局部视图在每个类别中呈现标签

That's your problem. 那是你的问题。 You should use an editor template: 您应该使用编辑器模板:

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

and then you should have the corresponding ~/Views/Shared/EditorTemplates/Tag.vbhtml template. 然后您应该具有对应的~/Views/Shared/EditorTemplates/Tag.vbhtml模板。 You don't need the EditTags.vbhtml partial. 您不需要EditTags.vbhtml部分。

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

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