2012-05-23 350 views
0

在我的網頁中,我需要根據名爲ButtonType的參數值填充按鈕。
比方說,如果​​那麼我需要隱藏每個按鈕,但butUpdate如何通過MVC操作方法顯示/隱藏HTML按鈕

我想知道如何通過MVC Action方法顯示/隱藏html按鈕。

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult SupplierDetail(int SupplierID, string ButtonType) 
    { 
     var Supplier = supplierListRepository.Supplier_SelectByID(SupplierID); 
     return View(Supplier); 
    } 

我正在使用Asp.net Mvc Razor窗體。

@using (Html.BeginForm("SupplierDetail_SubmitClick", "Supplier", FormMethod.Post, new { id = "frmSupplierDetail" })) 
{ 
@Html.ValidationSummary(true) 
<table cellpadding="0" cellspacing="0" border="0" style="width:450px; height:auto"> 
..... 
<tr> 
    <td>@Html.LabelFor(model => model.Phone)</td> 
    <td>@Html.EditorFor(model => model.Phone) 
     @Html.ValidationMessageFor(model => model.Phone)</td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td> 
     <input type="submit" id="butSave" name="butSave" value="Save" style="width:100px; height:auto" /> 
     <input type="submit" id="butUpdate" name="butUpdate" value="Update" style="width:100px; height:auto" /> 
     <input type="submit" id="butDelete" name="butDelete" value="Delete" style="width:100px; height:auto" /> 
     <input type="submit" id="butReset" name="butReset" value="Reset" style="width:100px; height:auto" /> 
    </td> 
</tr> 
</table> 
</div> 

<div id="content"> 
@Html.ActionLink("Back to List", "Index") 
</div> 

} 

每個建議將不勝感激。

+2

Appricated中,你已經意識到問題的答案爲20+同樣的意義問題你不打算接受答案? –

回答

1

的按鈕類型添加到可視數據:

ViewData["ButtonType"] = ButtonType 

然後,在視圖本身,你可以添加if/else語句,或者說適合所有的烏爾案件的任何其他邏輯,以決定如何處理渲染:

@if (ViewData["ButtonType"].ToString() == "Edit") 
{ 
    <input type="submit" id="butUpdate" name="butUpdate" value="Update"  
    style="width:100px; height:auto" />  
} 

當然,這不過是什麼可以做一個演示,宥要適應代碼UR樓內設有商務邏輯

+0

謝謝你非常明確的答案,@ YavgenyP –

2

顯示/隱藏按鈕不是控制器操作的責任。控制器操作不會/不應該知道按鈕的含義。這是一個觀點上存在的概念。另一方面,控制器操作應該與模型進行通信並準備一個視圖模型,以傳遞到視圖進行顯示。因此,您可以定義一個視圖模型,該模型將包含定義按鈕可見性的屬性,並基於ButtonType參數的值相應地設置這些屬性。然後,控制器操作會將此視圖模型傳遞給視圖,而不是您當前傳遞的supplier對象。顯然,視圖模型也將擁有一個財產來持有這個供應商。現在,所有剩下的視圖都是基於視圖模型屬性的值決定如何顯示按鈕。

在您的控制器
+0

非常感謝你的好建議,謝謝@Darin Dimitrov。 –