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

Related posts

Recent Viewed