Asp.net mvc MVC中的POST重定向GET中丢失的验证消息

Asp.net mvc MVC中的POST重定向GET中丢失的验证消息,asp.net-mvc,Asp.net Mvc,我有一个带有操作索引的ProductController(加载一个空白表单)。表单本身也是一个复杂的表单,表单元素(如下拉列表)显示已发布的值 代码如下 public ActionResult Index() { int id; id = Convert.ToInt32(Request.Form["ddlLendingType"]); if (id == 0) id = 1; ProductComm

我有一个带有操作索引的ProductController(加载一个空白表单)。表单本身也是一个复杂的表单,表单元素(如下拉列表)显示已发布的值 代码如下

 public ActionResult Index()
    {
        int id;
        id = Convert.ToInt32(Request.Form["ddlLendingType"]);
        if (id == 0)
            id = 1;
        ProductCommonViewModel viewData = new ProductCommonViewModel(_prodRepository.Method1(),_prodRepository.Method2())
        return View(viewData);
    }
当我从表单中单击submit时,它将保存产品,如果失败,它将显示验证错误消息

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Save(FormCollection fc)
    { 
        Product product = new Product();
        try
        {
           ...fill out all properties from form collection
            _prodRepository.SaveProduct(product);

            return RedirectToAction("Index", "Product");
        }
        catch (Exception ex)
        {
            TempData["Message"] = "An Error Occured while saving the product!";
            Validation.UpdateModelStateWithRuleViolation(product, ViewData.ModelState);
            // WHEN I call redirect to action Index on this view I can see the TempData variable but I cannot see validation summary and individual validation messages.How do I persist the msgs across requests?
        }

    } 
助手方法定义如下所示:

public static void UpdateModelStateWithRuleViolation(IRuleEntity entity, ModelStateDictionary dictModel)
    {
        List<RuleViolation> violations = entity.GetRuleViolations();

        foreach (var item in violations)
        {
            dictModel.AddModelError(item.PropertyName, item.ErrorMessage);
        }
    }
public static void UpdateModelStateWithRuleViolation(IRuleEntity实体,ModelStateDictionary模型)
{
列表冲突=entity.GetRuleViolations();
foreach(违反规定的var项目)
{
dictModel.AddModelError(item.PropertyName,item.ErrorMessage);
}
}

也将modelstate传递到tempdata

顺便说一句,与此相反:

 public ActionResult Index()
    {
        int id; //and here You could join declaration with assignment
        id = Convert.ToInt32(Request.Form["ddlLendingType"]);
您可以这样做:

 public ActionResult Index(int ddlLendingType)
        {

使用FormCollection是一种不好的做法,不应该使用。对于极端情况-创建自定义模型绑定器(具有非常好的绑定机制)或操作过滤器(`s source)。

也将modelstate传递到tempdata中

顺便说一句,与此相反:

 public ActionResult Index()
    {
        int id; //and here You could join declaration with assignment
        id = Convert.ToInt32(Request.Form["ddlLendingType"]);
您可以这样做:

 public ActionResult Index(int ddlLendingType)
        {

使用FormCollection是一种不好的做法,不应该使用。对于极端情况-创建自定义模型绑定器(具有非常好的绑定机制)或操作筛选器(`s source)。

我在跨多个请求保存TempData时遇到问题,我执行了以下操作,为每个重定向操作刷新TempData:

protected override RedirectToRouteResult RedirectToAction(string actionName, 
    string controllerName, System.Web.Routing.RouteValueDictionary routeValues)
{
    TempData["Notice"] = TempData["Notice"];
    TempData["Error"] = TempData["Error"];
    TempData["Warning"] = TempData["Warning"];
    return base.RedirectToAction(actionName, controllerName, routeValues);
}

我在跨多个请求保存TempData时遇到问题,我执行了以下操作,为每个重定向操作刷新TempData:

protected override RedirectToRouteResult RedirectToAction(string actionName, 
    string controllerName, System.Web.Routing.RouteValueDictionary routeValues)
{
    TempData["Notice"] = TempData["Notice"];
    TempData["Error"] = TempData["Error"];
    TempData["Warning"] = TempData["Warning"];
    return base.RedirectToAction(actionName, controllerName, routeValues);
}

如何从ActionResult Index()调用ActionResult Index(int)?为什么需要它?索引(int ddlendingtype)应替换为Index()。但是如果你需要它,那就没什么特别的了:“public ActionResult Index(){return Index(1);}”应该可以工作。我如何从ActionResult Index()调用ActionResult Index(int)?你为什么需要它?索引(int ddlendingtype)应替换为Index()。但如果您需要它,就没有什么特别的了:“public ActionResult Index(){return Index(1);}”应该可以工作。