2016-05-23 90 views
0

我想在用戶單擊按鈕時調用控制器方法,我還需要一個傳遞給此方法的變量。這是我的代碼。MVC - 調用按鈕單擊的控制器方法

查看:

<input type="number" id="nbrMetric" name=metricNbr class="form-control" style="width:75px;" min="0"> 

@Html.ActionLink("Search", "InputTest", "CustomersController", new { @class = "btn btn-info", @Metric = "nbrMetric" }) 

控制器:

public ActionResult InputTest(int Metric) 
      { 
       //Do Something 

       return View(); 
      } 
+1

它可能應該是的'客戶''而不是CustomersController'。 –

回答

2

你必須發回一個形式的源代碼是這樣的: 型號/ Yourmodel.cs:

public class FooModel 
    { 
     public int Metric { get; set; } 
    } 

你的控制器:

public ViewResult InputTest() { 

    return View(); 
} 

[HttpPost] 
public ActionResult SubmitInputTest(FooModel model) { 
    //your code 
    //do stuff 

    return RedirectToAction("index"); 
} 

您的頁面:

@model MVCWebApp.Models.FooModel 

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>InputTest</title> 
</head> 
<body> 
    <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
    <script src="~/Scripts/jquery.validate.min.js"></script> 
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 


    @using (Html.BeginForm("SubmitInputTest","Home", FormMethod.Post)) 
    { 
     @Html.AntiForgeryToken() 

     <div class="form-horizontal"> 
      <h4>FooModel</h4> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       @Html.LabelFor(model => model.Metric, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Metric, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Metric, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Save" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 
</body> 
</html> 
+0

你可以使用ajax tecnologies –

+0

雖然你的答案是正確的,但我沒有看到使用表單和POST請求的原因,在這種情況下,只要有querystring參數的簡單鏈接就足夠了,除非當然需要以支持頁面的非JS版本,在這種情況下,表單將是必要的。 – elolos

+0

因爲我想他需要通過輸入文本得到一個值 –

1

您可以在我已經把在評論中充分說明重複的問題看,你的情況這應該工作:

@Html.ActionLink("Search", "InputTest", "Customers", new { Metric = 3 }, new { @class = "btn btn-info" }) 

實際上這是Html.ActionLink的超載:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    RouteValueDictionary routeValues, 
    IDictionary<string, Object> htmlAttributes 
) 

https://msdn.microsoft.com/en-us/library/dd493068(v=vs.108).aspx

+0

我試過使用這段代碼,但我似乎每次都得到這個錯誤「參數字典包含一個空條目,用於方法'System.Web.Mvc.ActionResult InputTest的非空類型'System.Int32'的參數'Metric' (Int32)'in'FutatillDataAnalytics.Controllers.CustomersController'。可選參數必須是引用類型,可爲空類型,或者聲明爲可選參數 參數名稱:parameters「,它似乎沒有從中獲取值公制號碼輸入 –

+0

我現在明白你在做什麼。 'Html.ActionLink'不適合將參數從輸入發佈到控制器,您應該使用帶有提交按鈕的表單,而不是像C.Fasolin建議的那樣。 –

0

這將創建一個點擊都會重定向到一個控制器/動作(儀表板/索引)當按鈕

<button type="button" class="btn btn-default active" onclick="location.href='@Url.Action("Index", "Financial")'">Financial</button> 

這增加了發送參數和要求的能力:

<button type="button" class="btn btn-default active" onclick="location.href='@Url.Action("Financial", "KsDashboard", new { id = 257 }))'">Financial</button> 

這裏是生成的html:

<button class="btn btn-default" onclick="location.href='/KsDashboard/Financial/123'" type="button">Financial</button> 

基本上你所做的就是將@ Url.Action嵌入到按鈕的on單擊事件,其中@ Url.Action只是返回的URL controlleryou希望重定向到

MS Documention for @Url.action:

相關問題