MVC - 在按钮单击时调用控制器方法

时间:2016-05-23 13:41:12

标签: asp.net-mvc asp.net-mvc-4

我试图在用户单击按钮时调用控制器方法,我还需要传递给此方法的变量。这是我的代码。

查看:

 <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();
            }

3 个答案:

答案 0 :(得分:3)

你必须发回一个表格,如下所示: 型号/ 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>

答案 1 :(得分:1)

您可以在重复的问题中看到我已在评论中提供完整的解释,在您的情况下这应该有效:

angular

实际上这是@Html.ActionLink("Search", "InputTest", "Customers", new { Metric = 3 }, new { @class = "btn btn-info" }) 的重载:

Html.ActionLink

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

答案 2 :(得分:1)

这会创建一个按钮,单击该按钮将重定向到控制器/操作(仪表板/索引)

<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>

基本上你所做的就是在按钮的onclick事件中嵌入一个@ Url.Action,其中@ Url.Action只是将Url返回到你想要重定向到

的控件上。

MS Documention for @Url.action: