locked
How can I bring the  checkbox   value if ticked  as 1 else 0   to the controller model RRS feed

  • Question

  • User-1355965324 posted

    How can I bring the  checkbox   value if ticked  as 1 else 0   to the controller argument  variable _depotid   when I click submit button. Please can you help

    public class DepotUserLink
    {
    public List<int> DepotsID { get; set; }
    [NotMapped]
    public bool checkboxDepotLink { get; set; }
    public bool checkboxDefaultDepot { get; set; }
    
    }
    
    //In Controller
    HttpPost]
    public IActionResult DepotUserLink(DepotUserLink depotuserlink)
    {
       DepotsID  =  should be stored with selected userdepot Value
    }

    <tbody>
                                        @foreach (var _userDepot in ViewBag.UserDepots)
                                        {
                                            <tr>
                                                <td style="display:none;"  >
                                                    @_userDepot.Value
                                                    </td>
                                                <td>
                                                    @_userDepot.Text
    
                                                </td>
    
                                                <td>
                                                    <input type="checkbox" name="DepotLink" asp-for="checkboxDepotLink" />
                                                </td>
                                                <td>
                                                    <input type="checkbox" name="DefaultLink" asp-for="checkboxDefaultDepot" />
                                                </td>
                                            </tr>
                                        }
                                    </tbody>
    <div class="center-block  col-sm-6">
                                            <button type="submit" class="btn btn-primary form-control">Save</button>
                                        </div>
     $("#DepotCheck").click(function () {
    $('input[name="DepotLink"]:checkbox').not(this).prop('checked', this.checked);
    });

    Wednesday, December 5, 2018 10:56 AM

Answers

  • User1520731567 posted

    Hi polachan,

    For example,if you select DepotCheck, all checkbox named DepotLink will be selected,

    you just modify the controller,like:

          [HttpPost]
            public IActionResult DepotUserLink(DepotUserLink depotuserlink,string[] DepotLink)
            {
                return View();
            }

    string[] DepotLink will get all value of checkbox named DepotLink.

    if you would like to get their Id,you could modify,like:

    <input type="checkbox" name="DepotLink" value="@_userDepot.Id" asp-for="checkboxDepotLink" />
    

    Like the picture:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 6, 2018 6:05 AM