0

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?

1
  • An iframe might be useful in some situations but I think it could be used for the core functions of the system i.e loading, displaying modules as partials. If you have any other ideas I would like to hear them! Commented Jan 13, 2011 at 23:51

1 Answer 1

1

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.

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

Comments

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.