繁体   English   中英

将模型名称作为参数传递

[英]Pass model name as a parameter

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

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

我很难搞清楚你的​​追求。 话虽这么说,你尝试过使用反射吗?

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

此外,您可以使用Model.GetType().Name来获取模型对象名称并在需要的位置使用它。 这对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM