У меня есть два класса моделей для моего проекта:
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")
}
после того, как я нажму на кнопку «Создать формат». он возвращает false для ModelState.IsValid
и не добавляет никаких данных в мою базу данных
Что я должен сделать, чтобы кнопка «Создать формат» создала новую сущность для формата и сохранила ее в своей базе данных?
для Дополнительные мне нужно создать две модели на одной странице