简体   繁体   English

将模型名称作为参数传递

[英]Pass model name as a parameter

I have a partial view where I pass the model name to populate it, is there any way to pass model name as a parameter based on the controller action executed? 我有一个局部视图,我传递模型名称来填充它,有没有办法根据执行的控制器操作将模型名称作为参数传递?

<div id = "Details" >

<% List<Freshmen>() = model based on controller action executed %>

    <% using (Html.BeginForm("FreshmenDetails", "Students")) %>
        <% { %>
<%  Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
        <% } %>
</div>

Controller Action: 控制器动作:

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

I have 3 more Actions each for SophomoreDetails(),JuniorDetails(),SeniorDetails() 对于SophomoreDetails(),JuniorDetails(),SeniorDetails(),我还有3个动作

Currently I am displaying Partial View like this: 目前我正在显示像这样的部分视图:

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

I want something like: 我想要的东西:

<div id = "Details" >
    <% using (Html.BeginForm("FreshmenDetails", "Students")) %>
        <% { %>
<%  Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
        <% } %>

</div>

I am having a hard time figuring out exactly what your after. 我很难搞清楚你的​​追求。 That being said, have you tried using reflection? 话虽这么说,你尝试过使用反射吗?

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

Further more, you can use Model.GetType().Name to get the model object name and use it where you want. 此外,您可以使用Model.GetType().Name来获取模型对象名称并在需要的位置使用它。 That should work for you. 这对你有用。

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

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