简体   繁体   中英

Return view inside partial to full view on error

I have a wizard that loads up a partial view on each step. When the form generates an error it loads the error page. However it loads the FULL error page in the partial view and makes it look like its loading two pages at once. How can I redirect to the error page outside of the partial view?

I have this on the bottom of the step

            try
            {
                return PartialView("AccountSelection", vm);
            }
            catch (Exception ex)
            {
                return View("Error");
            }
        }
        return PartialView();

Personally, I'd handle errors at a higher level. You can specify an error page in your web.config file.

<configuration>
   <system.web>
      <customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
      </customErrors>
   </system.web>
</configuration>

If the goal is to have just a section of the page show an error message, then you could create a partial page named "Error" and save it in Views/Shared. Then change your code to...

        try
        {
            return PartialView("AccountSelection", vm);
        }
        catch (Exception ex)
        {
            return PartialView("Error");
        }
    }
    return PartialView();

Use RedirectToAction("ErrorAction","ErroController") . Based upon your needs, the action might be in a separate controller or in the same one(which I don't think would be right). You'll also need to customize your Error action method and view so that you can pass different messages for different circumstances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM