简体   繁体   中英

ASP.NET MVC How do I point a controller action to a different view?

I have a controller with a method that points to a view. How do I change the view that the action is mapped to? Like I want it to call ViewB instead of ViewA? Where do these mappings exist and how can I modify them? Thanks for any tips.

Thanks,
~ck in San Diego

Instead of:

return View(someModel);

use

return View("ViewYouWant", someModel);

To have a controller method redirect to a view that's not named the same as the action method, you can change the statement from

 return View();

to

 return View("ViewB");

You could also return a RedirectToAction("View") , or with Javascript

 json(new { Redirect = url.Action(action, data) }, JsonRequestBehavior.AllowGet);

and handle the return appropriately on the client side.

Happy Hunting!

您可以将视图名称传递给View方法: http//msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx

You can do the default:

return View(myModel);

Or specify the View Name under the same controller view folders or in shared:

return View("ThatView", myModel);

Or whatever view:

return View("~/myfolder/WhatEverView.ascx", myModel);

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