user676767
user676767

Reputation: 255

MVC return view() issue

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!

Upvotes: 2

Views: 1515

Answers (1)

Felix
Felix

Reputation: 10078

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

Upvotes: 1

Related Questions