tags:

views:

268

answers:

2

I have a ProductController with actions Index (which Loads a blank form). The form also posts to itself as its a complex form and the form elements like dropdowns show posted values the code is as follows

 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);
    }

When I click submit from the form, it saves the product and if it fails it should show the validation error messages.

 [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?
        }

    }

The helper method definition is as follows:

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

        foreach (var item in violations)
        {
            dictModel.AddModelError(item.PropertyName, item.ErrorMessage);
        }
    }
+2  A: 

Pass modelstate into tempdata too.

Btw, instead of this:

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

You can do this:

 public ActionResult Index(int ddlLendingType)
        {

And using FormCollection is a bad practice which should not be used. For extreme cases - create custom model binder (CodeCampServer has quite nice binding mechanism) or action filter (Kigg`s source).

Arnis L.
How do I call ActionResult Index(int) from ActionResult Index()??
chugh97
Why would you need that? Index(int ddlLendingType) is supposed to be replacement for Index(). But if you need that, there's nothing fancy: "public ActionResult Index(){ return Index(1); }" should work.
Arnis L.
+1  A: 

I had a problem with preserving TempData across multiple requests, I did the following, to refresh the TempData for every redirect action:

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);
}
Stuart