ASP.Net MVC checkbox values from view to custom controller method

后端 未结 2 1500
失恋的感觉
失恋的感觉 2021-01-24 16:20

I have a view with a table that displays my model items. I\'ve extracted the relevant portions of my view:

@model System.Collections.Generic.IEnumerable

        
2条回答
  •  再見小時候
    2021-01-24 16:35

    As @mattytommo said in comments, you should post your model to controller. It can be done with putting your checkbox inside a form. After clicking on button "Save and exit" all data from inputs inside this form will be serialized and sent to your controller where you can perform manipulations with session variables and so on. After that you can redirect wherever you like.

    Model

    public class YourModel
    {
        ...
        public bool IncludeProvision { get; set; }
        ...
    }
    

    View

    @model YourModel
    
    ...
    @using (Html.BeginForm("SaveAndSend", "Test", FormMethod.Post))
    {
        ...
        @Html.CheckBoxFor(model => model.IncludeProvision)
        ...
        
    }
    ...
    

    Controller

    public class TestController : Controller
    {
        ...
        [HttpPost]
        public ActionResult SaveAndSend(YourModel model)
        {
             if (ModelState.IsValid)
             {
                 // Some magic with your data
                 return RedirectToAction(...);
             }
    
             return View(model); // As an example
        }
        ...
    }
    

提交回复
热议问题