Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an actionresult to return both a view and a partial view

How can I use use an actionResult to return both a view and a partial view. Actually in case of an ajax request it should send a partial view else it should send a view.

   public ActionResult Test(string Name ="", DateTime? Date= null, string sex="" )
         {            
             myModel model = new myModel(Name, Date, Sex);
             if(IsAjaxRequest)
                 return PartialView("partialView", model)
             else
                 return View(model);
         }
like image 538
learning Avatar asked Feb 17 '11 07:02

learning


People also ask

Can I return ActionResult Instead of view results?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

How do I return two views from a controller?

You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel.

Can you use the View () method to return a partial view how?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

Which return type should be used when multiple ActionResult return types are possible in an action?

As you can see, the same action method “Index” is returning two different types named Content and View; if you want to return multiple types, you have to use base type as ActionResult.


1 Answers

if (Request.IsAjaxRequest())
    return PartialView("_Article", model);

return View(model);
like image 170
Lukáš Novotný Avatar answered Oct 13 '22 01:10

Lukáš Novotný