在主视图中创建复杂的模型多个零件 [英] Create multiple parts of complex model in the main view

查看:147
本文介绍了在主视图中创建复杂的模型多个零件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点新ASP.Net MVC,我有一个复杂的模型。

I'm a little new to ASP.Net MVC, I have a complex model.

public class BuildingPermit
{
    public int ApplicationID { get; set; }
    public virtual Person Applicant { get; set; }
    public virtual Area ApplicantArea { get; set; }
    public virtual ICollection<Owner> Owners { get; set; }
    /...
}

使用脚手架,我创建了控制器和所有的意见。不过,我要注册所有的细节在同一个页面,在寓意的 BuildingPermit 创建查看,创建细节为申请人类型 ApplicationArea 的键入等。有没有什么办法可以做到这一点?

Using scaffolding, I created the controller and all the views. However, I want to register all the details in the same page, meaning in the BuildingPermit's Create view, creating the details for Applicant of type Person, the ApplicationArea of type Area and so on. Is there any way I can accomplish this?

如果这是不可能的,我认为这是可能增加一个链接来创建对象。当用户点击它时,页面转到该视图,创建它,得到它的信息传回并显示在 BuildingPermit 的看法。

If it's not possible, I think it's possible to add a link to create the object. When the user clicks on it, the page goes to that view, creates it, get its information back and shows it in the BuildingPermit's view.

我想AP preciate你的帮助。

I'd appreciate your help.

推荐答案

您可以通过对人,面积,业主等创建一个模板编辑器实现这一点:

You could achieve this by creating an editor template for Person, Area, Owner etc in:

~/Views/Shared/EditorTemplates/Person.cshtml
~/Views/Shared/EditorTemplates/Area.cshtml
~/Views/Shared/EditorTemplates/Owner.cshtml

该编辑器模板将要被强类型的,并应给予该类型的编辑器布局:

The editor template will want to be strongly typed and should give the editor layout for the type:

@model Models.Person
<h2>Person</h2> 
<p>
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
</p>
<p>
    @Html.LabelFor(model => model.Address)
    @Html.EditorFor(model => model.Address)
</p>
// And so on

一旦你这样做的召 @ Html.EditorFor(型号=&GT; model.Applicant)。将拿起你的编辑视图中的模板,并显示

Once you've done this calling @Html.EditorFor(model => model.Applicant) will pick up your template and display within your Edit view.

如果你是想显示所有这些信息一起,那么你可能会想要也为这些类型的显示模板。这些工作一样,除非你编辑模板让你的模板在DisplayTemplates文件夹中。

If you are wanting to display all of this information together then you will probably want to also create display templates for these types. These work just like the editor templates except you keep your templates in a DisplayTemplates folder.

~/Views/Shared/DisplayTemplates/Person.cshtml
~/Views/Shared/DisplayTemplates/Area.cshtml
~/Views/Shared/DisplayTemplates/Owner.cshtml

这篇关于在主视图中创建复杂的模型多个零件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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