Reputation: 1030
I have 4 different ActionResult running in 2 different Controllers, I have created a partial view StudentList, I want to use same partialView as the model is same.
from 1st ActionResult I want to display StudentList by Class from 2nd ActionResult I want to display StudentList by Class Teacher from 3rd ActionResult I want to display StudentList by Fee Not Paid from 4th ActionResult I want to display StudentList by Absent Student
all 4 return a Model type of Student. with fields StudentName, ParentMobileNo
Is it possible not to create 4 different View and use single partial view or single view to display the result.
Regards
Upvotes: 2
Views: 1010
Reputation: 1
View should just worry about displaying model passed to it. How the model is created should be transparent to the view. In your case,you can have a single view which just displays StudentList model passed to it. For generating this model you can have either one action method or four of them. If you want single action method,you can pass a parameter indicating grouping.
Thanks Prasad
Upvotes: 0
Reputation: 999
Create a Partial View that will be hooked up to use your Student model.
Then create multiple actions in your controller for returning the different results.
StudentsByClass - then within this action call the relevant business layer/repository to do the query, as long as it's returning type Student (or the name of your model being used in your Partial View) it will be fine.
Then create another three actions for each of the scenarios, again calling the relevant business/repository method to do the query. Again as long as they return the same model that the Partial Student View is expecting it should work.
Then in each of the actions return the View along with the results to pass to the model like so:
return View("StudentList", model);
Upvotes: 0
Reputation: 3689
Yes. Create a shared view and pass the view name when returning the ActionResult from the controller.
return View("StudentList", model);
Or if you want to render a partial from a view:
@{ Html.RenderPartial("StudentList", model); }
Upvotes: 3