views:

363

answers:

3

I have a top-level page called ReceiveItem. Within that page, I have a couple different FORMs - only one of which will be filled out and submitted (depending upon how that item is received). This all works quite well from the UI perspective. Here is the general page structure:

<ReceiveItem.aspx>
    <ReceiveNewInventory.ascx>
    <ReceiveOrderReturn.ascx>
    <ReceiveFromLoan.ascx>

Except, I do not know how to properly display validation errors. Here is my controller for one of those forms:

    public ActionResult ReceiveNewInventory(
        int id,
        int vendorId,
        int quantity,
        decimal cost) {

        var db = new Data();
        var item = db.ItemSet.First(i => i.Id == id);
        var vendor = db.BusinessSet.First(i => i.Id == vendorId);

        ValidateCost(cost);
        ValidateQuantity(quantity);

        if (ModelState.IsValid) {
            item.AddNewInventory(vendor, quantity, cost);
            TempData["Message"] = "Added " + quantity + 
                " inventory items to " + item.FullDisplayName;
            return RedirectToAction("Index");
        }
        else {
            TempData["Quantity"] = quantity;
            TempData["VendorId"] = vendorId;
            TempData["Cost"] = cost;
            return RedirectToAction("ReceiveItem", new { id });
        }
    }

I would like to display the model errors that the two validation functions add using the simple Html.ValidationSummary function; but, those errors seem to get lost because I do the RedirectToAction. (My ReceiveNewInventory controller action does not have a view directly associated with it.)

With the one condition that I still want 1 page with multiple FORMs, what can I change about this design so that my validation messages show up on the ReceiveItem page?

+1  A: 

Look at NerdDinner and see how they do it. Very neat and you can display a summary at the top of the page as well as text next to each item if you wish.

let me know if you have trouble and I'll post code.

griegs
I see, NerdDinner is some MVC example. But it's very simple. I don't see any pages with multiple forms on them. Please be more specific as to where I should look, or post code. Thank you!
Frank Krueger
+1  A: 

You need to put the ModelState into TempData and extract it in your ReceiveItem action method. Alternatively, change the Redirect to a return View()

hth

Dan

Daniel Elliott
A: 

Why do you redirect to ReceiveItem even if you have errors? When you display the validation message, don't you want the user to have the opportunity to fix their mistakes? If so, why not keep them on the RecevieNewInventory page again and return the view?

James S
There is no ReceiveNewInventory page, just an action for the form.
Frank Krueger
Oh. I misread. Well, my statement still stands. Why redirect to ReceiveItem? Why not just show it as a view? As long as your form element names are qualified, it should work.
James S