0
Reply

mvc get unselected check boxes value from view to control

vipin mudgal

vipin mudgal

Mar 14 2016 12:15 PM
296
       I have a table MeetingResource and some feild that already store value 
      table feild:-
      Booking Id      MeetingResourcesID     IsActive 
       1                       1                                        false
       1                        2                                        true
        1                        3                                       false
        1                         4                                      true 
 
      i am fetching resources id  and Isactive value by checkboxs in view , i have applied foreach loop in       view to select all Isactive feild based on resources id. here all is ok but when i update some check         box checked or uncheked IsActive,then how to update check or uncheck value in database. save value like if i check then set isactive=1 and uncheck set isactive=0
 
    view:-
             
@using MAB_MVC.Models
@model MAB_MVC.Models.ResouceViewModel
@{
ViewBag.Title = "Do You Want to Attach Any resources";
}
<h2>@ViewBag.Title</h2>
<div class="row">
<div class="col-md-8">
<section id="ResourcesConfirmation">
@using (Html.BeginForm("MeetingAreaResources", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<form name="MeetingAreaResources" autocomplete="off" id="MeetingAreaResources" method="post">
@Html.AntiForgeryToken()
<hr />
<div class="form-group">
@*<div class="col-md-10">
<h4>Dear @User.Identity.Name,</h4>
</div>*@
@*Html.LabelFor(model => model.MeetingDate, new { @class = "col-md-2 control-label" })*@
<label class="text-info">
<br><h3>Meeting Area :@ViewBag.MeetingArea</h3>
</label>
<br />
<table class="table table-responsive table-condensed table-striped ui-sortable-handle">
<tr>
<th>
<label class="text-info">ResourceDescription</label>
</th>
<th><label class="text-info">IsActive <br>&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="chkAll" value="All" id="chkAll" onclick="SetAllCheckBoxes(this)" /></label><br/>
</th>
</tr>
@{
int cnt = 0;
List<MAB_MVC.Models.ResouceViewModel> Resources = ViewBag.ResourcePopulate;
if (Resources != null && Resources.Count() > 0)
{
foreach (var resource in Resources)
{
<tr>
<td>
@Html.DisplayFor(modelItem => resource.ResourceDescription)
@*<input type="text" disabled="disabled"
value="@resource.ResourceDescription" />*@
</td>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="MeetingResources" value="@resource.ResourceID" class="MeetingResources">
@*@Html.CheckBoxFor(modelItem => resource.IsActive)*@
</td>
</tr>
}
}
}
</table>
 
   Controller:-
         
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateMeetingAreaResources(FormCollection collection, string[] selectedResources)    // here i am getting only selected check box id in selected resources but i want                                              also uncheckbox id
{
MAB_MVC.Models.AreaResourceMapping ResourceMapping = new Models.AreaResourceMapping();
int UID = Convert.ToInt32(Session["UpdateID"]);
try
{
foreach (var mr in selectedResources)
{
//Here we have to apply code for update if i check or unckeck checkbox meeting resources set Isactive=1 and Isactive=0
 
string query = "update AreaResourceMappings set IsActive = 1 where ResourceID = " + mr + " and AreaID = " + UID + "";
db.Database.ExecuteSqlCommand(query);
}
db.SaveChanges();
ViewBag.ArearesourceMapping = UID;
// Session["ResourceMapping"] = ResourceMapping.AreaID;
return View("MeetingAreaFeedback", ResourceMapping);
 
 
    Model:-
         
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MAB_MVC.Models
{
//Written By vipin19012016 for Resources
public class ResouceViewModel
{
public int ResourceID { get; set; }
public string ResourceDescription { get; set; }
public bool IsActive { get; set; }
}
}