2

I'm very new to MVC, and am attempting to add further functionality to an app that has already been developed.

I'm having trouble returning a View with posted results after validation has rendered the model invalid.

My controller has three actions of note:

public ActionResult Create(int workID)

[HttpParamAction]
[HttpPost]
public ActionResult SaveNormal(WorkViewModel workView)

[HttpParamAction]
[HttpPost]
public ActionResult SaveAddAnother(WorkViewModel workView)

Because of previous requirement, I've had to change the submit action so that one of the two above are called with the posted results. Everything works fine except is I'm trying to post back due to the model state being invalid, which I'm attempting to with the following:

if (!ModelState.IsValid)
    return View("Create", workView);

It does take me to the create (Create.aspx) view, but the URL is Work/Action, rather than Work/Create, meaning that when I resave, Work/Action is not found.

I'm not sure if this is a routing issue, but have supplied routes below (which are basically the default I think...):

public static void RegisterRoutes(RouteCollection routes)
{

    // This is the original
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional 
        } // Parameter defaults
    );
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
}

Any help will be much appreciated!

1 Answer 1

1

the URL is based on the action name; not on the view name. You probably need to RedirectToAction("Create"...) and issue View() from there

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - I've redirected using TempData to store the model, although I'm losing the validation messages. Do I need to pass the ModelState in TempData?
The short answer is yes. The longer answer is that you may want to rethink the following decision: Because of previous requirement, I've had to change the submit action so that one of the two above are called with the posted results Try to have one submit action "Create" and have the logic there that checks the model state, Saves if appropriate, creates new (which is simply returning create a new empty view), etc. Seems much cleaner and simpler.
Ok, I've also passed the ViewData(which contains the invlaid modelstate) in the TempData. In answer to the rethink, the controller is set up like this because we wanted to have two buttons on the page, allowing for both to be clicked providing different actions, Saved (inserting and redirecting to other page), and SaveAndAddOther(inserts and reloads with fresh model). I seem to remember at the time that this was the only way to do it, but there could be another solution that I missed.
you absolutely can have two buttons leading to the same Post action. formData will have the name of the button that was actually clicked. but I realize, it's a different topic :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.