我有一个局部视图,我传递模型名称来填充它,有没有办法根据执行的控制器操作将模型名称作为参数传递?
<div id = "Details" >
<% List<Freshmen>() = model based on controller action executed %>
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
控制器操作:
public ActionResult FreshmenDetails(string id)
{
DataContext Student = new DataContext();
var FreshmenDetails = Student.Freshmen.Where(a => Convert.ToInt64(a.Id) == Convert.ToInt64(id)).ToList();
return PartialView("FreshmenDetails", FreshmenDetails );
}
我还有3个用于SophomoreDetails(),JuniorDetails(),SeniorDetails()
的动作目前我正在显示像这样的部分视图:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
<% using (Html.BeginForm("SophomoreDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SophomoreDetails", new List<Sophomore>()); %>
<% } %>
<% using (Html.BeginForm("JuniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("JuniorDetails", new List<Junior>()); %>
<% } %>
<% using (Html.BeginForm("SeniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SeniorDetails", new List<Senior>()); %>
<% } %>
</div>
我想要类似的东西:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
答案 0 :(得分:2)
我很难搞清楚你的追求。话虽这么说,你尝试过使用反射吗?
return PartialView(Model.GetType().Name, new { // Partial View object });
此外,您可以使用Model.GetType().Name
获取模型对象名称并在需要的位置使用它。
这应该适合你。