简体   繁体   中英

make controller actions' “return partialview()” automatically link to a “_partialview.cshtml” in asp.net mvc3

I'm writing a page in MVC3, and there are a few places where I want to request a page in ajax and from the url bar too.

If the request is a full pagerequest I want the Action to render "Example.cshtml" which is a full view. But is the request is an ajax request I would only want to render the "_Example.cshtml" which is a partial view.

My code is

    if (Request.IsAjaxRequest())
    {
        return PartialView("_Example");
    }
    return View();

but since the MVC3 is all about conventions and that everything is reconfigurable, I would like to able to write

    if (Request.IsAjaxRequest())
    {
        return PartialView();
    }
    return View();

and still load the "_Example.cshtml" if it's a partial view. I name "_Something.cshtml" all my partial views anyway so wouldn't it be cleaner if I could just call PartialView(); ?

Please tell me that this is possible. And tell me how.

EDIT:

I still want to be able to make the partialviews different from views, so switching the masterpage would be enough for only a few cases.

I would like to do something like overloading the default path to look for partialviews, like:

PartialViewLocationFormats = new[] {  
    "~/Views/{1}/_{0}.ascx",               
}; 

but this affect PartialView("Example") to use _Example.cshtml too, which is undesirable.

EDIT:

An other thing I tried is to overload the controller's PartialView() and PartialView(object model) methods, but they cannot be overridden neither can I find a proper way to find which action were they called from.

Have you seen this ? It may help with what you are looking for.

I wrote a pretty substantial answer on this one: Simple ASP.NET MVC CRUD views opening/closing in JavaScript UI dialog

Basically you adjust the view engine to open full "master themed" views for regular requests and then return the view, but with an empty master, when the request is done via ajax.

You could create an ActionFilterAttribute:

public class AjaxResultActionFilter: ActionFilterAttribute
{

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            var result = filterContext.Result as ViewResult;
            result.ViewName = "_" + result.ViewName;
            filterContext.Result = result;
        }
        else
        {
            base.OnResultExecuting(filterContext);
        }
    }

}

This will append a "_" infront of the ViewName that MVC tries to resolve if the IsAjaxRequest returns true.

All you need to do now is to decorate either your actions or the controller with this attribute:

    [AjaxResultActionFilter]
    public ActionResult Index()
    {
        return View("Index");
    }

Now, I wrote this on the fly, so in this version it only works when you actually pass the ViewName into the return View() statement.

With some debugging and more time though, I'm sure this could be fixed :)

Hope this helps!

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