0

I am trying to send back the checkbox value back to the controller. The checkbox does not recognise the change in the checkboxfor and keep sending the values it got when I generated the page.

Could anyone explain where I have got it wrong? I have looked at few examples and unfortunately they do not fullfill my requirements.

My Class

public class SomeClass
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool IsEnabled { get; set; }
}

My Controller

    [HttpPost]
    public ActionResult Index(Guid id,bool check = false)
    {
        try
        {
            _importService.EnableDisable(id, check);
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            LogError(ex.Message, ex);
            throw;
        }
    }

Razor Code

  <html>
<head>
    <title>Values</title>
</head>
<body>
<div>
    <h4>Current Values</h4>
    <table>
        @foreach (var m in Model)
        {
            <tr>
                <td><a href="@Url.Action("ExtraInfo", "MyAction", new {id = m.Id})">@m.Name</a></td>
                <td>                 
                    @using (Html.BeginForm("Index", "MyAction", new { id = m.Id, check = m.IsEnabled }, FormMethod.Post, null))
                    {
                        @Html.HiddenFor(m => SomeClass.Id)
                         @Html.CheckBoxFor(m => SomeClass.IsEnabled)

                        <input type="submit" value="Save" />
                    }
                </td>
            </tr>
        }
    </table>
</div>
</body>
</html>

The Save button appears like this

Yuvi
  • 528
  • 8
  • 18
  • You property is named `IsEnabled` so the method needs to match - `public ActionResult Index(Guid id,bool IsEnabled)`. But why are you generating multiple forms in a loop - you can only post back one at a time. Have one for and use a `for` loop so you can post all changes at once (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Sep 12 '16 at 13:01
  • You also need to remove `new { id = m.Id, check = m.IsEnabled }` from `BeginForm()` –  Sep 12 '16 at 13:03
  • There is one request being send at a time. Save button is generated alongside each object. Removing the new part will not send the id and the value of the checkbox. – Yuvi Sep 12 '16 at 13:15
  • What is the point of this? A user can click multiple checkboxes and click a submit button thinking that they are updating them (and probably wont even realize that the changes are not being made). Look at the link I gave you, or use ajax to post the values. Your implementation is not only confusing, its awful performance. And have just realized you have `@Html.HiddenFor(m => import.Id)` - it would need to be `@Html.HiddenFor(model => m.Id)` etc (and what image are you referring to?) –  Sep 12 '16 at 13:19
  • The idea was to give ability to the user to update tickbox for a single Id rather than update all the tickbox. – Yuvi Sep 12 '16 at 13:21
  • But ya, I am going through the link that you have suggested. I am trying it. – Yuvi Sep 12 '16 at 13:24
  • FYI. An HTML checkbox only sends a value if it is checked. Otherwise, it is not included in the post body *at all*. – Chris Pratt Sep 12 '16 at 14:04

0 Answers0