ASP.net MVC - 使用EditorFor与相同型号的两倍 [英] ASP.net MVC - Using EditorFor with the same model type twice

查看:151
本文介绍了ASP.net MVC - 使用EditorFor与相同型号的两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC以下模型:

public class IndexViewModel
{
    public SubViewModel SubViewModel { get; set; }
}

在我的索引视图我做以下,显示为编辑 SubViewModel

In my Index view I am doing the following to display an Editor for SubViewModel:

Index.cshtml

@model IndexViewModel

@Html.EditorFor(m => m.SubViewModel)

在我的 SubViewModel.cshtml 文件我试着做到以下几点:

In my SubViewModel.cshtml file I tried to do the following:

EditorTemplates / SubViewModel.cshtml

@model SubViewModel

@Html.EditorForModel("SubViewModelForm")

基本上,我想裂开我的编辑器分为两部分。第一位编辑模板会创建表单,和内部人会创造一切实际的表单字段。的问题是,内视图从未得到呈现。

Basically, I want to split apart my Editor into two pieces. The first editor template would create the form, and the inner one would create all of the actual form fields. The problem is that the inner view is never getting rendered.

这就像MVC意识到,我已经叫EditorFor在该模型是$ P $的老毛病又犯了pventing我。有没有一种方法可以让我得到这个工作?

It's like MVC realizes that I already called EditorFor on that model and is preventing me from doing it again. Is there a way I can get this to work?

修改

我想我打发生了什么事清楚,但我想不会。

I thought I typed out what was happening clearly but I guess not.

我想用两个模板来显示一个单一的模式。在第一个模板,我想呈现的形式和提交按钮,和一些容器div的。

I want to use two templates to display a single model. In the first template, I want to render the form and the submit button, and a few container divs.

在第二个模板,我想使所有的表单字段。

In the second template, I want to render all of the form fields.

Index.cshtml     @ Html.EditorFor(M => m.SubViewModel) - >渲染的形式和容器

Index.cshtml @Html.EditorFor(m => m.SubViewModel) --> render the form and container

SubViewModel.cshtml     @ Html.EditorForModel(SubViewModelForm) - >渲染表单字段

SubViewModel.cshtml @Html.EditorForModel("SubViewModelForm") --> render the form fields

问题是,第二次调用(@ Html.EditorForModel(SubViewModelForm))似乎并没有做任何事情。它永远不会呈现任何标签都没有。这是过得去MVC忽略。

The problem is that the second call (@Html.EditorForModel("SubViewModelForm")) doesn't seem to do anything. It never renders any tags at all. It is getting ignored by MVC.

我想这样做,这种方式的原因是因为我将要发布这种形式有一个Ajax调用。如果一切正常(模型状态是有效的),那么我想JSON数据返回到视图。如果模型是无效的,我要回只用表单域的局部视图。这样我就不需要连上我所有的事件处理程序了。我只需更换整个视图的表单字段来代替。

The reason I want to do it this way is because I am going to be posting this form with an ajax call. If everything is okay (model state is valid) then I want to return JSON data to the view. If the model is invalid, I want to return the partial view with just the form fields. This way I don't need to hook up all of my event handlers again. I can just replace the form fields instead of the entire view.

推荐答案

阅读您的更多详细信息后,我相信这将是最好使用一个容器模板的模板。

After reading your additional details I believe it would be best to use a container template template.

/* Container.cshtml (container markup and call to editor template) */
<fieldset>                    
    @Html.EditorForModel()
</fieldset>

/* SubViewModel.cshtml editor template */
@model SubViewModel

@Html.DisplayFor(x => x.Property1)
@Html.EditorFor(x => x.Property1)
...

/* Index.cshtml */
@Html.RenderPartial("Container.cshtml", Model.SubViewModel)

这篇关于ASP.net MVC - 使用EditorFor与相同型号的两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆