ctb
ctb

Reputation: 131

MVC 3 Dynamic routing issue with partial views

I'm writing a CMS system to learn MVC and was wondering if it's possible in MVC to get a partial view with a form to post to a controller without the URL changing. It would be similiar idea to a code behind in webforms.

Say I have a comment module (displayed in a partial view) and want to add a comment, I don't want the URL to change. Currently it redirects to /Comment/Add.

I think this is possible with ajax? But was also looking for an option for situations were ajax isn't possible.

Is there any when to do this or will the posting of a form always cause a redirect?

Upvotes: 0

Views: 637

Answers (1)

Tom Chantler
Tom Chantler

Reputation: 14931

I'm not sure I understand the question correctly, but what about calling return RedirectToAction("actionName", "controllerName"); or similar (basically sending them back to the main post again) at the end of the action where you post your comment?

And of course you could customise it so it doesn't do this if you're using Ajax.

e.g. this sort of thing

    if (Request.IsAjaxRequest())
    {
        // Just return the comment string or whatever it is
        // and insert it into the page as required.
        return Json(s);
    }
    else
    {
        // Redo the page.
        return RedirectToAction("actionName", "controllerName");
    }

I've done this sort of thing in the past and it works.

Upvotes: 1

Related Questions