0

I am trying to pass clicked button to the controller when user clicked Next button from the view/FregihtTool.cshtml

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

}

Now I have FreightController looks like this:

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

I want to pass CategoryNum from the view and the controller handles it and tell which FreightClass it is.

1
  • Assuming I understood - you're probably better off with a radio or checkbox (depending on what you need). If you need to stick to button, you're using Angular so that would be how you'd "persist" that value (and will handle the submit similarly in client side/Angular). Hth. Commented Feb 10, 2017 at 18:23

1 Answer 1

1

Name your buttons, e.g.:

<button class="col-md-3" type="button" id="PrintedMaterials" name="categoryNum" value="1" class="btn btn-secondary " ng-click="levelOU()">Printed Materials </button>
Sign up to request clarification or add additional context in comments.

2 Comments

No.. it does not work.. I my submit button on the bottom correct? <input type="submit" value="Next" name="categoryNum" class="btn btn-success" />
No. Exactly as I showed in my answer: each of your buttons with values corresponding to your switch statement should have that name. That actually means that final submit button is irrelevant. The only way to post a value from a button is to have it submit as well. If you don't want to submit immediately when the user clicks one of those options, then you need to use something like a radio control instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.