使用list.netmvc为多模型创建视图和控制器

本文关键字:创建 视图 控制器 模型 list netmvc 使用 | 更新日期: 2023-09-27 18:28:21

我用这种方式制作了一个实体,

public class UserTest : IdentityEntity
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Email { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Fax { get; set; }
    public virtual DateTime BirthDate { get; set; }
    public virtual IList<UserAddress> Address { get; protected set; }
}
public class UserAddress : IdentityEntity
{
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string Province { get; set; }
}

每次我将一个创建视图与一个模型相关联,但这一次(我想知道下一次会有很多时间)我需要创建一个具有多个实体的创建视图,就像这种情况一样。

使用脚手架工具,VS不会为该列表准备视图。如何继续?

使用list.netmvc为多模型创建视图和控制器

我们的应用程序中有类似的功能,允许管理员创建新用户。模型与您的相似。为了在"创建用户"视图中呈现地址列表,我们使用了"显示"answers"编辑器"模板。

这些模板类似于局部视图,存储在"DisplayTemplates"或"EditorTemplates"文件夹下的Shared文件夹中。

Shared''DisplayTemplates''AddressViewModel.cshtml

@model Eda.RDBI.Web.Models.AddressViewModel
<h3>@Model.BillingType.ToString() Address</h3>
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.OrganizationId)
@Html.HiddenFor(model => model.BillingType)

<div class="editor-label">
    @Html.LabelFor(model => model.ContactFirstName)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.ContactFirstName)
    @Html.ValidationMessageFor(model => model.ContactFirstName)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.ContactLastName)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.ContactLastName)
    @Html.ValidationMessageFor(model => model.ContactLastName)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Street1)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Street1)
    @Html.ValidationMessageFor(model => model.Street1)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Street2)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Street2)
    @Html.ValidationMessageFor(model => model.Street2)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.City)
    @Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.State)
    @Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Country)
    @Html.ValidationMessageFor(model => model.Country)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Zip)
    @Html.ValidationMessageFor(model => model.Zip)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Email)
    @Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.PhoneNumber)
    @Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Extension)
</div>
<div class="editor-field">
    @Html.DisplayFor(model => model.Extension)
    @Html.ValidationMessageFor(model => model.Extension)
</div>

Shared''EditorTemplates''AddressViewModel.cshtml

它具有类似的元素,但使用EditorFor。我不会把它粘贴在这里,因为它是一个大代码。

一旦你在正确的文件夹中有了模板,在你的主视图中,例如User''Create.cshtml,以及其他html元素,如用户名、年龄等,都有下面的行:

        <div class="row">
            @Html.EditorFor(m => m.Addresses)
        </div>

例如,您可以在这里在线找到如何在mvc中使用Display或Editor模板的示例。有关模板的更多帮助,请参阅此链接。.

希望这能有所帮助。