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);
}
When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.
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.
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.
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.
if (Request.IsAjaxRequest())
return PartialView("_Article", model);
return View(model);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With