mercredi 15 avril 2020

How do I make an ASP.NET MVC checkbox in the view trigger an action method in the controller?

I am looking for a method in my controller to be triggered when the user clicks on a checkbox in my view.

I have looked at similar answered question regarding this however, none of the solutions have worked.

My view markup is as follows:

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(Model => Model.Answers)
        </th>
        <th>
            @Html.DisplayName("Number of Votes")
        </th>
        <th>
            @Html.DisplayName("Tick to Vote")
        </th>
    </tr>

    @foreach (var item in Model.Answers)
    {
        <tr>
            <td>
                @item.Answer
            </td>
            <td>
                @item.AnsCount
            </td>
            <td>
                <div class="checkbox">
                    @Html.CheckBox(item.Answer, item.Active, new { @class = "checkbox" })
                </div>
            </td>
        </tr>
    }
</table>

My controller code is as follows:

public ActionResult AddVote(PollAnswersModel answer)
        {
            if (answer.Active.Equals("True"))
            {
                answer.Active = true;
                answer.AnsCount =+ 1;
            }
            return Redirect(Request.UrlReferrer.PathAndQuery);
        }

In the Controller, I am looking to add on a vote to the 'AnsCount' variable when the user clicks on a checkbox. It will then refresh the page to show the new amount of votes an option will have.




Aucun commentaire:

Enregistrer un commentaire