2011-06-15 148 views
0

您好我是MVC剃鬚刀新手MVC剃刀按鈕

我正在試圖做一個頁面,3個按鈕插入,刪除和更新。

我的觀點看起來像

 <div>  
      @using (Html.BeginForm()) 
      { 

       <input type="button" class="button1" value="Insert" /> 
       <input type="button" class="button1" value="Delete" /> 
       <input type="button" class="button1" value="Update" /> 
      } 
     </div> 

我的控制器就像是

 public ActionResult Index() 
     { 
       return View(); 
     } 

我將如何得到這些按鈕的事件在我的控制器,這樣我就能寫出邏輯每個按鈕。請幫助我獲得有助於解決此問題的編碼或任何適當的鏈接。

感謝 聖

回答

2

[編輯]

重新itterate我在其他答案的評論點:當您綁定的邏輯在C#中你要綁定你的C#代碼,該語言的按鈕的值。

想象一下,你在英文版本的保存按鈕:

<input type="submit" value="Insert" name='button' /> 

,並在你的代碼,你會使用值切換:

public ActionResult Index(string button) 
{ 
    switch (button) 
    { 
     case "Insert": ... 
     case "Update": ... 
     case "Delete": ...  
    } 

    return View(); 
} 

現在 - 這種形式被觀察時,用另一種語言 - 你認爲會發生什麼?!?!

這裏是威爾士HTML輸出:

<input type="submit" value="Mewnosod" name='button' /> 

和德國:

<input type="submit" value="Einfügen" name='button' /> 

如何有史以來去上班?

全球化不是一個單獨的問題!!!!

你的動作會是這樣的,如果你用這個方法:

public ActionResult Index(string button) 
{ 
    switch (button) 
    { 
     case "Insert": ... 
     case "Update": ... 
     case "Delete": ...  
     case "Einfügen": ... 
     case "Mewnosod": .... 
     .... a load of other languages for each action type - 
    } 

    return View(); 
} 

請認真... ....

[/編輯]

這裏是我的MVC動作選擇代碼:Asp.Net Mvc action selector

實質上你需要一個動作選擇器類:

/// <summary> 
/// AcceptParameterAttribute to enable submit buttons to execute specific action methods. 
/// </summary> 
public class AcceptParameterAttribute : ActionMethodSelectorAttribute 
{ 
    /// <summary> 
    /// Gets or sets the value to use in submit button to identify which method to select. This must be unique in each controller. 
    /// </summary> 
    public string Action { get; set; } 
  
    /// <summary> 
    /// Determines whether the action method selection is valid for the specified controller context. 
    /// </summary> 
    /// <param name="controllerContext">The controller context.</param> 
    /// <param name="methodInfo">Information about the action method.</param> 
    /// <returns>true if the action method selection is valid for the specified controller context; otherwise, false.</returns> 
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
    { 
        if (controllerContext == null) 
        { 
            throw new ArgumentNullException("controllerContext"); 
        } 
  
        var req = controllerContext.RequestContext.HttpContext.Request; 
  
        return req.Form.AllKeys.Contains(this.Action); 
  
    } 
} 

哪個作品的名字你給按鈕。

您可以再與裝飾操作:

[AcceptParameter(Action = "Edit")] 
public ActionResult Person_Edit(PersonViewModel model){ 
... 
} 

切換動作是髒 - 這是一個更清潔的方式。我覺得也更自然。

+0

這裏是該代碼與列表(數據綁定)支持的更新版本:http://blogs.sonatribe.com/wayne/2011/06/15/171/ – iwayneo 2011-06-15 07:53:38

+0

我試過這個......但我是得到一個錯誤信息像「Sample.Controllers.AcceptParameterAttribute」不包含一個構造函數,它帶有1個參數「 – san 2011-06-15 09:19:08

+0

ooops抱歉!我的錯誤 - 你需要使用可選的args方法:[AcceptParameter(Action =「Edit」)] – iwayneo 2011-06-15 09:21:11

5

給該按鈕的名稱:

<div>  
@using (Html.BeginForm()) 
{ 
    <input type="button" name="button" class="button1" value="Insert" /> 
    <input type="button" name="button" class="button1" value="Delete" /> 
    <input type="button" name="button" class="button1" value="Update" /> 
} 
</div> 

和使用,在你的行動:

public ActionResult Index(string button) 
{ 
    switch (button) 
    { 
     case "Insert": ... 
     case "Update": ... 
     case "Delete": ...  
    } 

    return View(); 
} 

與所有的說,我不會用這種方法。我將動態修改表單動作到不同的控制器動作(使用Url.Action),將代碼掛鉤到使用jQuery單擊處理程序。

+3

+1只是爲了最後那句話。這就是爲什麼我討厭網絡形式 - 不是因爲技術,因爲事件驅動型思維如何被洗腦.net網絡開發人員,我們完全忘記了HTTP的全部內容。 MVC的開發人員需要基本上忽略他們很多Web形式的思維視角。 – RPM1984 2011-06-15 07:56:47

+0

-1因爲開關太髒!雖然我對上述評論+1) – iwayneo 2011-06-15 07:59:15

+1

如果網站是多語言的,這個選項也會中斷 - Web是不是什麼? – iwayneo 2011-06-15 08:00:33

0

我建議你閱讀一些關於MVC3的簡單教程 - 它會讓你有機會理解MVC框架的基本原理。你可以開始here

現在你的問題:最好的辦法是從每個按鈕調用控制器中的一些動作。

@Html.ActionLink("Insert", "Insert") 

@Html.ActionLink("Edit", "Edit", new{Id=lineId}). 

然後,當用戶點擊這個鏈接 - 他會去正確看待,這是準備來處理任務。

+0

您認爲這會對行動起作用嗎? HTTP GET不是POST嗎? – iwayneo 2011-06-15 07:46:58

+0

爲什麼你認爲需要POST? – VikciaR 2011-06-16 12:08:55

+0

如何將表單字段發送到服務器?你是否誤解了OP的意圖? – iwayneo 2011-06-16 14:29:03

0

這裏得到改善AcceptParameterAttribute,如果您使用的本地化和我一樣

public class AcceptParameterAttribute : ActionMethodSelectorAttribute 
{ 
    public string Name { get; set; } 
    public Type ResourceType { set; get; } 
    public string Value { get; set; } 

    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) 
    { 
     HttpRequestBase request = controllerContext.RequestContext.HttpContext.Request; 
     if (ResourceType != null) 
     { 
      ResourceManager resourceManager = new ResourceManager(ResourceType); 
      return request.Form[Name] == resourceManager.GetString(Value); 
     } 
     return request.Form[Name] == Value; 
    } 
} 

用法類似於DisplayAttribute

[AcceptParameter(ResourceType = typeof(Resources), Name = "submit", Value = "Search")] 

和按鈕

<input type="button" name="submit" value="@Resources.Search" />