【问题标题】:How to reference multiple models in same MVC view [duplicate]如何在同一个 MVC 视图中引用多个模型 [重复]
【发布时间】:2016-03-23 23:47:30
【问题描述】:

我一直在尝试剖析自动化 MVC 视图和控制器,并对其进行修改,以便我可以将视图中的表单中的信息插入到两个单独的数据库表中(首先是实体框架数据库)。

这是我到目前为止所拥有的。如果我自己引用顶部的任何一个命名空间,但不是一起引用,它会起作用吗?

@model manage.mysite.DataModels.Property_Type
@model manage.mysite.DataModels.Room_Type

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) 
{

<div class="form-horizontal">
    <h4>Property_Type</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.PropertyType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PropertyType, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PropertyType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.RoomType, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.RoomType, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.RoomType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

【问题讨论】:

    标签: asp.net-mvc entity-framework model-view-controller razor


    【解决方案1】:

    不能在一个视图中引用多个模型。

    但是,您可以创建具有其他两个模型作为属性的复合模型。

    这样:

    using manage.mysite.DataModels;
    
    namespace manage.mysite.ViewModels
    { 
        public class FooViewModel
        {
            public Property_Type PropertyType { get; set; }
            public Room_Type RoomType { get; set; }
        }
    }
    

    然后改为服务这个模型。

    @model manage.mysite.ViewModels.FooViewModel
    
    @{
        ViewBag.Title = "Create";
    }
    
    <h2>Create</h2>
    
    @using (Html.BeginForm()) 
    {
        <div class="form-horizontal">
            <h4>Property_Type</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.PropertyType.PropertyType, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.PropertyType.PropertyType, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.PropertyType.PropertyType, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.RoomType.RoomType, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.RoomType.RoomType, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.RoomType.RoomType, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    

    另见:

    MVC Multiple Models in One View

    how to create a view with multiple models mvc 4?

    Which is the best way to use multiple models with sames properties and using a unique view?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多