Asp.net mvc 如何在同一MVC视图中引用多个模型
我一直在尝试剖析一个自动化的MVC视图和控制器,并对其进行修改,以便能够将视图中表单的信息插入到两个单独的数据库表中(首先是实体框架数据库) 这是我到目前为止所拥有的。如果我单独引用顶部的任何一个名称空间,但不同时引用它们,它会起作用吗Asp.net mvc 如何在同一MVC视图中引用多个模型,asp.net-mvc,entity-framework,model-view-controller,razor,Asp.net Mvc,Entity Framework,Model View Controller,Razor,我一直在尝试剖析一个自动化的MVC视图和控制器,并对其进行修改,以便能够将视图中表单的信息插入到两个单独的数据库表中(首先是实体框架数据库) 这是我到目前为止所拥有的。如果我单独引用顶部的任何一个名称空间,但不同时引用它们,它会起作用吗 @model manage.mysite.DataModels.Property_Type @model manage.mysite.DataModels.Room_Type @{ ViewBag.Title = "Create"; } <h2>
@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>
}
@model manage.mysite.DataModels.Property\u类型
@模型管理.mysite.DataModels.Room\u类型
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
属性类型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.PropertyType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PropertyType,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PropertyType,“,new{@class=“text danger”})
@LabelFor(model=>model.RoomType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.RoomType,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.RoomType,“,new{@class=“text danger”})
}
不可能在一个视图中引用多个模型
但是,您可以创建复合模型,该模型将其他两个模型作为属性
因此:
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>
}
@model manage.mysite.ViewModels.FooViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
属性类型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.PropertyType.PropertyType,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.PropertyType.PropertyType,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.PropertyType.PropertyType,”,新的{@class=“text danger”})
@LabelFor(model=>model.RoomType.RoomType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.RoomType.RoomType,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.RoomType.RoomType,”,新的{@class=“text danger”})
}
另见:
一个视图中不可能参考多个模型 但是,您可以创建复合模型,该模型将其他两个模型作为属性 因此:
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>
}
@model manage.mysite.ViewModels.FooViewModel
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
属性类型
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.PropertyType.PropertyType,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.PropertyType.PropertyType,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.PropertyType.PropertyType,”,新的{@class=“text danger”})
@LabelFor(model=>model.RoomType.RoomType,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.RoomType.RoomType,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.RoomType.RoomType,”,新的{@class=“text danger”})
}
另见: