从另一个模型视图将数据插入数据库
Posted
技术标签:
【中文标题】从另一个模型视图将数据插入数据库【英文标题】:Insert data into database from another models view 【发布时间】:2020-02-29 17:37:26 【问题描述】:我的项目有两个模型类,
public class BookModel
public Int64 ISBN get; set;
public DateTime AddedDate get; set;
public bool isActive get; set;
public int Quantity get; set;
public int? FormatID get; set;
public virtual FormatModel Format get; set;
和
public class FormatModel
public FormatModel()
Books = new HashSet<BookModel>();
public int ID get; set;
public string Value get; set;
public virtual ICollection<BookModel> Books get;
如果指定的格式不存在,我想在我的Create.cshtml
文件中创建一个模式来添加新格式。
如何为我的模态创建一个提交按钮以在FormatModel
中添加格式?
这是我的看法:
@model RookBookMVC.Models.BookModel
@
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.ReturnUrl = "/Book/Create";
<h2>Create</h2>
@using (Html.BeginForm("Create", "Book", FormMethod.Post))
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>BookModel</h4>
<hr />
@Html.ValidationSummary(true, "", new @class = "text-danger" )
<div class="form-group">
@Html.LabelFor(model => model.FormatID, "FormatID", htmlAttributes: new @class = "control-label col-md-2" )
<div class="input-group col-md-10">
@Html.DropDownList("FormatID", null, "Select Book Format", htmlAttributes: new @class = "form-control" )
<div class="input-group-append">
<button type="button" class="btn btn-secondary" data-toggle="modal" data-target="#exampleModalCenter">
Add New
</button>
</div>
</div>
@Html.ValidationMessageFor(model => model.FormatID, "", new @class = "text-danger" )
</div>
<div class="form-group">
@Html.LabelFor(model => model.ISBN, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.ISBN, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.ISBN, "", new @class = "text-danger" )
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Quantity, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Quantity, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Quantity, "", 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>
@using (Html.BeginForm("Create", "Format", FormMethod.Post))
@Html.AntiForgeryToken()
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Add New Format</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
@Html.LabelFor(model => model.Format.Value, htmlAttributes: new @class = "control-label col-md-2" )
<div class="col-md-10">
@Html.EditorFor(model => model.Format.Value, new htmlAttributes = new @class = "form-control" )
@Html.ValidationMessageFor(model => model.Format.Value, "", new @class = "text-danger" )
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" value="Create Format" class="btn btn-default" />
</div>
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
@Scripts.Render("~/bundles/jqueryval")
点击“创建格式”按钮后。它为 ModelState.IsValid
返回 false 并且不会将任何数据添加到我的数据库中
我应该怎么做才能使“创建格式”按钮为格式创建一个新实体并将其保存在我的数据库中?
对于 Additional 我需要通过单个页面创建两个模型
【问题讨论】:
您必须将 BookModel 的强制属性作为隐藏属性传递,或者在模态表单帖子中传递带有属性名称的参数。 @Jaggan_j 我需要将格式添加为新实体,并且用户必须在插入新格式后从下拉列表中选择图书 【参考方案1】:您可能必须创建一个视图模型并将视图模型属性作为参数传递给操作 - 格式/创建帖子。 ViewModel 属性:
public string FormatValue get; set;
模态视图:
@Html.EditorFor(model => model.FormatValue, new htmlAttributes = new @class = "form-control" )
参数与从模态表单传递的视图模型属性同名的控制器代码:
[HttpPost]
public ActionResult Create(string formatValue)
using (var dataContext = new YourDbContextModel())
FormatModel fmtModel = new FormatModel();
fmtModel.Value = formatValue;
// rest of your code to save data
【讨论】:
谢谢,我应该将我的 Viewmodel 传递给 Controller 吗? ViewModel 属性是否包含 Book 和 FormatValue? 不是整个 ViewModel,只是上面显示的属性【参考方案2】:public class MYViewModel public string FormatValue get; set;
创建.cshtml
@model MYViewModel
<div>
@Html.EditorFor(model => model.FormatValue, new htmlAttributes = new @class = "form-control" )
</div>
你的意思是这样的吗? 或
public class MYViewModel
public string FormatValue get; set;
Public BookModel Book get; set;
【讨论】:
ViewModel 的目的往往是在同一个视图中包含多个模型,所以你的第二个是正确的。以上是关于从另一个模型视图将数据插入数据库的主要内容,如果未能解决你的问题,请参考以下文章