2016-11-24 93 views
0

我有3個頁面/網站:一個首頁,一個產品頁面和一個佈局頁面。 我在頂部有一個導航欄:Start(帶您到首頁)和一個帶有「所有產品」元素的下拉菜單。 enter image description here 如果您點擊「所有產品」,您將被引導至Products-Page(products.cshtml)。可選GET參數

products.cshtml(page)應該接收一個可選的GET參數「category」(type = string)。如果輸入此參數,它應該只顯示產品,其中的類別不在字符串中。 例如字符串=海鮮=>只顯示產品類別=海鮮

到目前爲止我的代碼在我layout.cshtml:

<ul class="dropdown-menu"> 
<li method="get" name="kategorie" target="_blank" > 
<a href="~/Produkte.cshtml">Alle Speisen</a> 
</li> 
</ul> 

我不知道我怎麼聲明,GET參數是可選的,以及如何宣佈只顯示字符串中類別的產品。

對不起英文不好

+0

我無法正常unserstand。 根據我的理解你想...,你有類別的下拉,如果你從下拉菜單中選擇任何類別,你會根據你選擇的類別重定向到產品頁面..我是嗎? –

+0

@Mohit Solanki我編輯它,我希望理解現在更好 – noclue123

回答

0

如果你的目標是在菜單中顯示的餐點列表,你可以將列表與UI分離,然後做出菜單中的每個條目的條目。這裏是一個小例子:

模型類:

public class MealsViewModel 
{ 
    public string Category {get; set;} 
    public string DisplayName {get; set;} 
    public int Id {get; set;} 
} 

控制了Methode:

public ActionResult index(string category) 
{ 
    var model = GetListofMeals(category); 
    model.insert(0, new MealsViewModel(){ id=0, DisplayName="Alle Speisen", Category = ""}; 
    return View(model); 
} 


public ActionResult Products(int id) 
{ 
    var model = GetMeal(id); 
    return View(model); 
} 

private List<MealsViewModel> GetListofMeals(string category) 
{ 
    ...... 
} 

private MealsViewModel GetMeal(int id) 
{ 
    ....... 
} 

索引視圖:

@model IEnumerable<Models.MealsViewModel> 

<ul class="dropdown-menu"> 
@foreach(var meal in Model) 
{ 
<li> 
@Html.ActionLink(Model.DisplayName, "products", new { id = Model.id}, new { target ="_blank"}) 
</li> 
} 
</ul> 

維耶爾SPASS北Ausprobieren;)

有樂趣嘗試