我有简单的形式。当我使用发布请求提交空表单(用于测试验证)时 ...
它不显示验证消息,也不返回状态代码 302。 并生成一些随机查询字符串。像这样:
http://localhost:49852/Events/Create?Type=0&details=System.Collections.Generic.List%601%5BSampleProject.Models.Type%5D
这是我从未见过的状态!
@{Html.BeginForm("Save", "Event" , FormMethod.post);}
@Html.ValidationSummary(true, "Please fix these problems.")
// fields...
<input type="submit" value="Save" class="btn btn-primary" />
@{Html.EndForm();}
Controller
[Authorize]
[HttpPost]
public ActionResult Save(EventFormViewModel viewModel)
{
if (!ModelState.IsValid)
return RedirectToAction("Save",viewModel);
//..additional Code.
return RedirectToAction("Index", "Home");
}
最佳答案
问题是,如果 ModelState
是 InValid
,您将重定向到带有模型数据的 Save
方法,而不是返回带有无效值的当前 View 模型数据。
所以编写你的Save
POST
方法如下:
[Authorize]
[HttpPost]
public ActionResult Save(EventFormViewModel viewModel)
{
if (!ModelState.IsValid)
return View(viewModel);
//..additional Code.
return RedirectToAction("Index", "Home");
}
关于c# - ASP.NET 提交表单 - 找到状态代码 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53816131/