c# - 从另一个模型视图将数据插入数据库
问题描述
我的项目有两个模型类,
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")
}
单击“创建格式”按钮后。它返回 falseModelState.IsValid
并且不向我的数据库添加任何数据
我应该怎么做才能使“创建格式”按钮为格式创建一个新实体并将其保存在我的数据库中?
对于其他我需要通过一个页面创建两个模型
解决方案
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;}
}
推荐阅读
- javascript - 为什么javascript indexOf方法给我错误的输出
- excel - 在 excel 中,如何根据另一张表的条件自动填充一张表?
- python - pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime:越界纳秒时间戳:9999-12-31 00:00:00
- nestjs - 将信息从 NestJS 中间件传递到控制器/服务
- android - recyclerview添加元素时不添加元素的问题
- vpn - 作为 vpn 连接到远程桌面
- python - 在 Python 中过滤嵌套字典
- javascript - 如何在本机基础的组件框之间创建空白
- rest - Rest API 的对应物
- excel - 将不同范围的单元格从 Excel 发送给不同的人