【问题标题】:Show success or failure message on form.submit()在 form.submit() 上显示成功或失败消息
【发布时间】:2014-11-24 11:35:09
【问题描述】:

由于 form.Submit() 将在 mvc 中提交整个 for。 如果我想在提交时显示成功或失败如何显示。 我不想在临时数据中保留成功状态并将其传递给表单的 Get 方法

控制器:

[HttpGet]
public void TestAction()
{
  return View()
} 

[HttpPost]
public void TestAction(someData)
{
  // Store data to DB 
  return View()
} 

Script :

$("#btnSave").click(function(){
form.Submit();
});

【问题讨论】:

  • 而不是提交表单使用event.preventDefault()并进行ajax调用并通过form.serialize()发送表单数据,然后从httppost testaction actionresult发送一个json令牌值,无论数据是否保存并显示该令牌alert() 中的值。
  • 如果你使用ajax提交,返回视图是没有意义的。只需返回一个包含消息的 Json 并更新 DOM 以显示它,否则将消息添加到 ViewBag 属性并在视图中呈现它
  • 我不想使用 Ajax 的人
  • 那么我认为没有任何方法可以满足您的要求...通过简单的表单提交页面将回发并刷新您无法停止的...
  • 然后只需将消息添加到ViewBag

标签: jquery asp.net-mvc asp.net-mvc-3 jquery-ui asp.net-mvc-4


【解决方案1】:

你可以试试这个http://jameschambers.com/2014/06/day-14-bootstrap-alerts-and-mvc-framework-tempdata/

[HttpPost]
public ActionResult Create(Person person)
{
    if (ModelState.IsValid)
    {
        _people.Add(person);
        Success(string.Format("<b>{0}</b> was successfully added to the database.", person.FirstName), true);
        return RedirectToAction("Index");
    }
    Danger("Looks like something went wrong. Please check your form.");
    return View(person);
}

【讨论】:

  • AddAlert 正在使用 TempData,因为我提到我不想使用 TempData
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多