views:

105

answers:

1

In a small web app I'm making for internal use, I want the user to be able to select one of the data files to open. I have a strongly typed view which takes the list of files as the model.

My initial version had an Index action which got the list of data files and returned the view, then a Create action for creating a new file, and an Upload action for uploading an existing file. However, in both cases it would be a bad user experience to have a whole new page just for a single text box and button, so I added two forms to the Index view, one pointing to Create, the other to Upload, both using the Post verb.

This works perfectly fine, plus still retains nice URL naming - even though Create/Upload don't have UIs. The problem is that at the end of both of these actions, I do a return RedirectToAction("Index"); Which in turns gets the data to be displayed and shows the view - fine. But I get no validation. It seems that in order to get the ValidationSummary and ValidationMessage helpers to work, I need the Post action to be called the same as the Get action (Index, in this case). I changed everything so that I just have a Get/Post Index action, and an if statement in the Post action to see if the button was Create or Upload. But this seems like a far clunkier solution than the one I originally came up with.

The other option, of course, is to create views for Create/Upload, each with the single text box - but I'd rather hve the clunky code than the clunky UI.

Is there anything I'm missing, or is this just the way the MVC validation framework is designed.

A: 

It looks as if you realy want this application to be one page where all the stuff is going on. That seams reasonable because it is very small. In this case I would stick with the one action you have, because validation works out of the box.

The other option you have is to put the Errormessages into TempData in Upload/Create an use them in Index (by putting them into ModelState).

Malcolm Frexner
Thanks - I agree, this is probably the easiest way to go for now. It feels a bit less elegant than I'd like, but there are several more involved alternatives, like the one you recommended, which would be worth it for a larger application - thanks.
Saqib