How to save checkbox checked values in Database

Posted by user1298215 on Stack Overflow See other posts from Stack Overflow or by user1298215
Published on 2012-03-29T10:20:34Z Indexed on 2012/03/29 11:30 UTC
Read the original article Hit count: 209

Filed under:

How to save checkbox values in database.

Below is my view code.

@foreach (var item in Model)    
{    
    @Html.CheckBox("statecheck", (IEnumerable<SelectListItem>)ViewData["StatesList"])
    @Html.DisplayFor(modelItem => item.state_name)    
    </br>    
}
<input class="ASPbutton" type="submit" value="submit"/>

Below is My controller.

public ActionResult States()  
{
    ViewData["StatesList"] = new SelectList(am.FindUpcomingStates().ToList(), "state_id", "state_Name");

    return View();   
}

My model is

public IQueryable<state> FindUpcomingStates()
{
    return from state in Adm.states orderby state.state_name select state;
}

After clicking submit button checked item state_id will be saved into database.

I wrote like below in Controller, but i got true or false values, i want state_id [AcceptVerbs(HttpVerbs.Post)] public ActionResult States(string _stateName, char[] statecheck, FormCollection formvalues) {
statecheck = Request.Form["statecheck"].ToArray(); ViewData["StatesList"] = new SelectList(am.FindUpcomingStates222().ToList(), "state_id", "state_id", _stateName); }

© Stack Overflow or respective owner

Related posts about asp.net-mvc-3