2017-05-04 16 views
0

我創造在這裏使用MVC 5我有兩個按鈕確定被點擊了MVC的按鈕5

<div class="row"> 
    <div class="col-sm-offset-5 col-sm-1"> 
     <input type="submit" name="save" value="SAVE" class="btn btn-primary glyphicon glyphicon-save" /> 
    </div> 
    <div class="col-sm-offset-0 col-sm-1"> 

      <input type="submit" name="reset" id="reset" value="Reset" class="btn btn-warning active glyphicon glyphicon-refresh" /> 
    </div>       
</div> 

和我有一個正在被點擊按鈕之後調用方法的Web應用程序,

現在對這一事件我想區分的按鈕點擊,

一樣,

如果用戶點擊

<input type="submit" name="save" value="SAVE" class="btn btn-primary glyphicon glyphicon-save" /> 

然後

{ 
//this code should run 
} 

,如果用戶點擊

<input type="submit" name="reset" id="reset" value="Reset" class="btn btn-warning active glyphicon glyphicon-refresh" /> 

然後

{ //this set of code should run } 

,我的方法是這樣的

[HttpPost] 
     public ActionResult insertrecd(FormCollection fc) 
     { 
      if(fc["reset"]==null) 
      { 
       return RedirectToAction("party", "party"); 
      } 
      else 
      { 
       ViewBag.message = string.Format("Hello {0}.\\nCurrent Date and Time: {1}", "name", DateTime.Now.ToString()); 
       return RedirectToAction("party", "party"); 
      } 
     } 

我需要做什麼,我想對不同的按鈕點擊做不同的代碼?

回答

3

給每個輸入按鈕的名稱相同,但不同的值

<input type="submit" name="action" value="save" class="btn btn-primary glyphicon glyphicon-save" /> 

<input type="submit" name="action" id="reset" value="reset" class="btn btn-warning active glyphicon glyphicon-refresh" /> 

值將然後是你的形式收集作爲

fc["action"] 

所以,你的控制器動作可能看起來像

[HttpPost] 
public ActionResult insertrecd(FormCollection fc) 
{ 
    var action = fc["action"]; 

    if(action == "save") 
    { 
     //save stuff 
    } 

    if(action =="reset") 
    { 
     //reset stuff 
    } 
}