gpt4 book ai didi

asp.net-mvc - 如何将模型传递给具有其他参数的 Action

转载 作者:行者123 更新时间:2023-12-02 13:42:49 24 4
gpt4 key购买 nike

我的问题是,当我在DeleteEmployee中执行catch部分时,如何将ModelStateErrors发送到操作Employee

   public ActionResult Employee(int ID, string Name)
{
EmployeeListModel model = new EmployeeListModel (ID, projectName);
return View(model);
}

public ActionResult DeleteEmployee(Employee emp)
{

try
{
emp.Delete();
return RedirectToAction("Employee", new { ID = emp.ID, Name = emp.Name });
}

catch (Exception e)
{
EmployeeListModel model = new EmployeeListModel (emp.ID, emp.Name);
ModelState.AddModelError("Error", e.Message);
return RedirectToAction("Employee", model);
}
}
<小时/>

带 return View("Employee", model);我仍然无法将 ID 和名称作为参数发送。

最佳答案

使用 TempData 在多个 Controller 操作中持久保存 ModelState

MvcContrib 有一个操作过滤器可以执行此操作。 Jeremy Skinner 编写了代码并在 http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/ 上发表了一篇关于它的博客文章。 。那里的源链接已损坏,因此我发布了下面的代码。

ModelStateToTempDataAttribute Source Code

/// <summary>
/// When a RedirectToRouteResult is returned from an action, anything in the ViewData.ModelState dictionary will be copied into TempData.
/// When a ViewResultBase is returned from an action, any ModelState entries that were previously copied to TempData will be copied back to the ModelState dictionary.
/// </summary>
public class ModelStateToTempDataAttribute : ActionFilterAttribute
{
public const string TempDataKey = "__MvcContrib_ValidationFailures__";

/// <summary>
/// When a RedirectToRouteResult is returned from an action, anything in the ViewData.ModelState dictionary will be copied into TempData.
/// When a ViewResultBase is returned from an action, any ModelState entries that were previously copied to TempData will be copied back to the ModelState dictionary.
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var modelState = filterContext.Controller.ViewData.ModelState;

var controller = filterContext.Controller;

if(filterContext.Result is ViewResultBase)
{
//If there are failures in tempdata, copy them to the modelstate
CopyTempDataToModelState(controller.ViewData.ModelState, controller.TempData);
return;
}

//If we're redirecting and there are errors, put them in tempdata instead (so they can later be copied back to modelstate)
if((filterContext.Result is RedirectToRouteResult || filterContext.Result is RedirectResult) && !modelState.IsValid)
{
CopyModelStateToTempData(controller.ViewData.ModelState, controller.TempData);
}
}

private void CopyTempDataToModelState(ModelStateDictionary modelState, TempDataDictionary tempData)
{
if(!tempData.ContainsKey(TempDataKey)) return;

var fromTempData = tempData[TempDataKey] as ModelStateDictionary;
if(fromTempData == null) return;

foreach(var pair in fromTempData)
{
if (modelState.ContainsKey(pair.Key))
{
modelState[pair.Key].Value = pair.Value.Value;

foreach(var error in pair.Value.Errors)
{
modelState[pair.Key].Errors.Add(error);
}
}
else
{
modelState.Add(pair.Key, pair.Value);
}
}
}

private static void CopyModelStateToTempData(ModelStateDictionary modelState, TempDataDictionary tempData)
{
tempData[TempDataKey] = modelState;
}
}

这里有一些类似的帖子

关于asp.net-mvc - 如何将模型传递给具有其他参数的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5104567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com