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