C# 将模型状态错误传递给其他操作方法
我的行动方法如下:C# 将模型状态错误传递给其他操作方法,c#,asp.net-mvc,modelstate,C#,Asp.net Mvc,Modelstate,我的行动方法如下: [HttpPost] public ActionResult Edit(EditViewModel model) { if (ModelState.IsValid) { //Do Something } var model1 = new IndexViewModel(); ModelState.AddModelError("", "An error occurred while editi
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
if (ModelState.IsValid)
{
//Do Something
}
var model1 = new IndexViewModel();
ModelState.AddModelError("", "An error occurred while editing the user.");
return RedirectToAction("Index", model1);
}
关于验证错误,我希望它与模型状态错误一起传输到下面的方法
[HttpGet]
public ActionResult Index(IndexViewModel model)
{
IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
}
<div class="row">
<div class="col-xs-12">
@Html.ValidationSummary("", new { @class = "alert alert-danger validation" })
</div>
</div>
[HttpGet]
公共行动结果索引(IndexViewModel模型)
{
IEnumerable allErrors=ModelState.Values.SelectMany(v=>v.Errors);
}
My index.cshtml定义了一个验证摘要以显示模型状态错误
[HttpGet]
public ActionResult Index(IndexViewModel model)
{
IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
}
<div class="row">
<div class="col-xs-12">
@Html.ValidationSummary("", new { @class = "alert alert-danger validation" })
</div>
</div>
@Html.ValidationSummary(“,new{@class=“alert-danger-validation”})
是否有一种方法可以将模型状态错误从编辑方法传递到索引方法,并将其加载到索引屏幕上?当前代码不起作用。allErrors字段为空,不包含任何添加的错误。如您所见,将ViewModel传递到
RedirectToAction()
不会保留模型错误。正如@Shyju在本文中提到的,RedirectToAction()
helper方法导致发出新的GET请求。但是,您可以使用TempData
将对象持久化到下一个操作方法。您可以使用以下代码来实现此目的:
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
TempData["EditViewModel"] = model;
if (ModelState.IsValid)
{
//Do Something
}
var model1 = new IndexViewModel();
ModelState.AddModelError("", "An error occurred while editing the user.");
return RedirectToAction("Index", model1);
}
[HttpGet]
public ActionResult Index(IndexViewModel model)
{
model = TempData["IndexViewModel"] as IndexViewModel;
IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
}
[HttpPost]
公共操作结果编辑(EditViewModel模型)
{
TempData[“EditViewModel”]=模型;
if(ModelState.IsValid)
{
//做点什么
}
var model1=新的IndexViewModel();
AddModelError(“,”编辑用户时出错。“);
返回重定向操作(“索引”,模型1);
}
[HttpGet]
公共行动结果索引(IndexViewModel模型)
{
model=TempData[“IndexViewModel”]作为IndexViewModel;
IEnumerable allErrors=ModelState.Values.SelectMany(v=>v.Errors);
}
有关详细信息,您可以查看此项。如您所见,将ViewModel传递到
RedirectToAction()
不会持续存在模型错误。正如@Shyju在本文中提到的,RedirectToAction()
helper方法导致发出新的GET请求。但是,您可以使用TempData
将对象持久化到下一个操作方法。您可以使用以下代码来实现此目的:
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
TempData["EditViewModel"] = model;
if (ModelState.IsValid)
{
//Do Something
}
var model1 = new IndexViewModel();
ModelState.AddModelError("", "An error occurred while editing the user.");
return RedirectToAction("Index", model1);
}
[HttpGet]
public ActionResult Index(IndexViewModel model)
{
model = TempData["IndexViewModel"] as IndexViewModel;
IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
}
[HttpPost]
公共操作结果编辑(EditViewModel模型)
{
TempData[“EditViewModel”]=模型;
if(ModelState.IsValid)
{
//做点什么
}
var model1=新的IndexViewModel();
AddModelError(“,”编辑用户时出错。“);
返回重定向操作(“索引”,模型1);
}
[HttpGet]
公共行动结果索引(IndexViewModel模型)
{
model=TempData[“IndexViewModel”]作为IndexViewModel;
IEnumerable allErrors=ModelState.Values.SelectMany(v=>v.Errors);
}
有关更多信息,您可以查看此信息。请查看。这就是它的人生目标。看看。这就是它的人生目标。