0

我有一个部分视图,我在其中传递模型名称来填充它,有没有办法根据执行的控制器操作将模型名称作为参数传递?

<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 );
    }

SophomoreDetails()、JuniorDetails()、SeniorDetails() 各有 3 个动作

目前我正在显示这样的部分视图:

<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>
4

1 回答 1

2

我很难弄清楚你到底想要什么。话虽如此,您是否尝试过使用反射?

return PartialView(Model.GetType().Name, new { // Partial View object });

此外,您可以使用Model.GetType().Name获取模型对象名称并在需要的地方使用它。那应该对你有用。

于 2013-08-06T17:50:04.090 回答