我的项目有两个模型类,
data.frame
和
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,并且不向我的数据库添加任何数据
如何制作“创建格式”按钮以创建新的格式实体并将其保存在数据库中?
对于其他,我需要通过一个页面创建两个模型
答案 0 :(得分:0)
您可能必须创建一个视图模型,然后将视图模型属性作为参数传递给“操作-格式/创建帖子”。 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
}
}
答案 1 :(得分:0)
public class MYViewModel{ public string FormatValue { get; set; } }
Create.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;}
}