开发者

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 lookin开发者_StackOverflow中文版g 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜