开发者

ASP.NET MVC: View gets rendered in an alert window

开发者 https://www.devze.com 2023-02-07 01:55 出处:网络
My view gets rendered in an alert window.I have a post action that adds a new record to my repository, and then returns a list of matching objects for display:

My view gets rendered in an alert window. I have a post action that adds a new record to my repository, and then returns a list of matching objects for display:

    [HttpPost]
            public ActionResult Add(FormCollection collection)
            {
    ...
    _repository.AddMyObject(myobject);
    _repository.Save()
    _matchingResults = _repository.GetMatchingResults(myobject);

    if (Request.IsAjaxRequest())
        return View("Results", _matchingResults );
    ...
}

"Results" is a view that renders a list of matchingResults. However, all I get is an alert window with the rend开发者_如何学编程ered html. I can't use RedirectToAction because I need to pass in _matchingResults.

Any suggestions?


Your view rendering the results should be a partial view i.e. Results.ascx (user control) and then you would return that to the view via return PartialView("Results", _matchingResults)


One work around although ugly might be to use Tempdata to store what you want and retrieve it in your Results action as TempData persists between two requests

However TempData can only store strings but Phil Haacked comes to a rescue with this blog.

A common pattern when submitting a form in ASP.NET MVC is to post the form data to an action which performs some operation and then redirects to another action afterwards. The only problem is, the form data is not repopulated automatically after a redirect. Let's look at remedying that, shall we?

0

精彩评论

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