【发布时间】: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 我需要将格式添加为新实体,并且用户必须在插入新格式后从下拉列表中选择图书
标签: c# asp.net-mvc entity-framework model-view-controller