Multiple forms on one view in ASP.Net MVC while keeping validation

In a small web application 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 that takes a list of files as a model.

My original version had an index action that fetched a list of data files and returned a view, then a New action to create a new file, and a Load action to load an existing file. However, in both cases, it would be a bad user experience to have a whole new page for just one text box and button, so I added two forms to the Index view, one pointing to New and the other pointing to Load as using the Message verb.

This works great, plus it still retains a nice URL name even though Create / Upload has no UIs. The problem is that at the end of both of these actions I do return RedirectToAction ("Index"); Which in turn receives data for display and shows the view - great. But I am not getting confirmation. It seems that in order to work with the ValidationSummary and ValidationMessage helpers, I need the Post action to get the same name as the Get action (in this case, Index). 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 much harsher solution than the one I originally came up with.

Another option, of course, is to create views for Create / Upload, each containing one text field, but I'd rather use this clunky code than a clunky interface.

Is there something I am missing or is this just how the MVC validator framework is designed.

+2


source to share


1 answer


It sounds like you really want this app to be one page where everything happens. This seam is reasonable because it is very small. In this case, I would stick with the one action you have, because the validation is done out of the box.



Another option you have is to put Errormessages in TempData in Upload / Create, use them in Index (by putting them in ModelState).

0


source







All Articles