C# 使用MVC局部视图时,数据未传递到SQL Server
我在局部视图中有一个弹出对话框。当用户单击“创建”按钮时,将显示一个弹出对话框,在弹出对话框中是一个表单,用户在其中键入信息,然后将信息保存到数据库中。我让一切正常,没有任何错误,但我键入的数据没有保存到数据库中 请帮忙,我是编程新手,谢谢 索引页:C# 使用MVC局部视图时,数据未传递到SQL Server,c#,jquery,asp.net-mvc,asp.net-mvc-4,C#,Jquery,Asp.net Mvc,Asp.net Mvc 4,我在局部视图中有一个弹出对话框。当用户单击“创建”按钮时,将显示一个弹出对话框,在弹出对话框中是一个表单,用户在其中键入信息,然后将信息保存到数据库中。我让一切正常,没有任何错误,但我键入的数据没有保存到数据库中 请帮忙,我是编程新手,谢谢 索引页: <li>@Html.Partial("_Create")</li> 在我的家庭控制器中,我将ActionResult创建(从脚手架)更改为_创建: // GET: Home/Creat
<li>@Html.Partial("_Create")</li>
在我的家庭控制器中,我将ActionResult创建(从脚手架)更改为_创建:
// GET: Home/Create
public ActionResult _Create()
{
return View();
}
// POST: Home/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")] Question question)
{
if (ModelState.IsValid)
{
db.Questions.Add(question);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(question);
}
请试着换衣服
@using (Html.BeginForm())
{
}
到
@使用(Html.BeginForm(“_Create”,“Home”,FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Title,htmlAttributes:new{@class=“controllabel col-md-1”})
@EditorFor(model=>model.Title,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Title,“,new{@class=“text danger”})
}
并查看调试器在主控制器中使用值调用_Create(post)方法是否能够在_Create(post)方法中获取模型值?请放一个断点并检查。我把断点放在public ActionResult\u Create(httpPost)上,但没有命中。我不确定是否需要在部分视图中使用Ajax作为弹出对话框窗体。请尝试下面的答案,我已更改了您的窗体,添加了操作名称、控制器名称和FormMethod。还添加了一个提交按钮eah,它可以工作。非常感谢。
// GET: Home/Create
public ActionResult _Create()
{
return View();
}
// POST: Home/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")] Question question)
{
if (ModelState.IsValid)
{
db.Questions.Add(question);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(question);
}
@using (Html.BeginForm())
{
}
@using (Html.BeginForm("_Create","Home",FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-11">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
<input type="submit" value="submit"/>
</div>
</div>
</div>
}