如何使用ASP.NET MVC将单击的按钮值传递给控制器​​?

时间:2017-02-10 16:53:04

标签: asp.net asp.net-mvc twitter-bootstrap razor

当用户从视图/ FregihtTool.cshtml中单击$robotest = $_POST['robotest']; if($robotest){ //is a robot proceed no further }else{ //is a person } 按钮时,我试图将单击的按钮传递给控制器​​

Next

}

现在我的FreightController看起来像这样:

@model ThanksTrucking.Models.Shipping


<h2>FreightTool</h2>


@using (Html.BeginForm("FreightTool", "Shipping")) 

{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Calculator</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div row>
        <div class="col-md-12">
            <button class="col-md-3" type="button" id="PrintedMaterials" value="1" class="btn btn-secondary " ng-click="levelOU()">Printed Materials </button>
            <button class="col-md-3" type="button" id="Machinery" value="2" class="btn btn-secondary " ng-click="levelCD()">Machinery</button>
            <button class="col-md-3" type="button" id="PaperGoods" value="3" class="btn btn-secondary " ng-click="levelD()">Paper Goods </button>
            <button class="col-md-3" type="button" id="MetalWood" value="4" class="btn btn-secondary " ng-click="levelF()">Metal & Wood Materials </button>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-12 text-right">
            <input type="submit" value="Next" class="btn btn-success" />
        </div>
    </div>
</div>

我想从视图中传递// GET: Shipping/FreightTool public ActionResult FreightTool() { return View(); } // Post: Shipping/FreightTool [HttpPost] [ValidateAntiForgeryToken] public ActionResult FreightTool(int categoryNum) { int FreightClass = 0; switch (categoryNum) { case 1: FreightClass = 50; break; case 2: FreightClass = 60; break; case 3: FreightClass = 70; break; case 4: FreightClass = 80; break; default: FreightClass = 0; break; } return RedirectToAction("Create", "Shipping", FreightClass); } ,控制器会处理它并告诉它CategoryNum

1 个答案:

答案 0 :(得分:1)

为您的按钮命名,例如:

<button class="col-md-3" type="button" id="PrintedMaterials" name="categoryNum" value="1" class="btn btn-secondary " ng-click="levelOU()">Printed Materials </button>