【发布时间】:2019-08-05 18:42:06
【问题描述】:
我是 MVC Asp.Net 的新手,我正在处理一个简单的 Web 应用程序,但我正在尝试使用 Modals 对其进行动态编码(创建新记录),但问题是当我调用部分视图时,代码不保存信息。
这是模态框的 HTML。
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@Html.Partial("View", new MyWebAppInMVC.Models.Question())
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
这里是部分页面:
@model MyWebAppInMVC.Models.Question
@using (Html.BeginForm("Index", "Questions", FormMethod.Post, new { id = "AddModal" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Question</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DateOfQuestion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateOfQuestion, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateOfQuestion, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" id="BtnSubmit" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")}
这里是控制器
// GET: Questions/Create
public ActionResult Create()
{
return View();
}
// POST: Questions/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Title,Description,DateOfQuestion,Type")] Question question)
{
if (ModelState.IsValid)
{
db.Questions.Add(question);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(question);
}
其实很简单,但是局部视图并没有保存任何东西。 我的代码有什么问题?
提前致谢。
【问题讨论】:
-
您可以将您的控制器代码添加到帖子中吗?
-
没问题。
-
如果您在
Create发布操作中设置断点,它会被命中吗?我猜不是因为您的表单发布到Questions操作.. 或者您可能只是没有发布相关代码,这很难说。从您发布的内容来看,您的表单 using 声明应该是@using (Html.BeginForm("Create", "Questions", FormMethod.Post, new { id = "AddModal" }))
标签: c# asp.net asp.net-mvc web