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";
}
@ViewBag.Title
@using (Html.BeginForm("MeetingAreaResources", "Admin", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.AntiForgeryToken()
@* *@
Dear @User.Identity.Name,
@*Html.LabelFor(model => model.MeetingDate, new { @class = "col-md-2 control-label" })*@
Meeting Area :@ViewBag.MeetingArea
@Html.DisplayFor(modelItem => resource.ResourceDescription) @* value="@resource.ResourceDescription" />*@ | @*@Html.CheckBoxFor(modelItem => resource.IsActive)*@ |
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; }
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.