2013-08-06 157 views
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); 
    } 

我必須每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

你的目標是? – Pluc

+0

發佈你的模型和你的控制器動作。 – ataravati

+0

@Pluc我有多個部分視圖顯示在相同的位置,但有不同的控制器,動作和模型。我能夠參數化Controller和Action的名字,但是在參數化模型中掙扎,尋找一種將這些傳遞給局部視圖的有效方法。這可以實現嗎? – user793468

回答

2

我有一個困難時期後,你到底是什麼搞清楚。這就是說,你有沒有嘗試過使用反射?

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

更進一步,你可以使用​​得到模型對象名稱和您要使用它。 這應該適合你。