In my Create action I have given many checkbox for selecting items for eg, suppose I have 5 checkbox items and I selected 3 out of that and these selected checkboxes are stored in db and here my problem is, from the index page when I click on the edit link I want to list the entire checkbox items ie, 5 and also want to show the checked checkbox as checked in edit page ie, the 3 items that is checked, so that the user can change the selected checkbox and these changed items must add to db
controller
// GET: MappedName/Edit/5
public async Task<ActionResult> Edit(int? id )
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
MappedName mappedName= await db.MappedNames.FindAsync(id);
var ListItems = (from ap in db.MappedNames
join a in db.Masters on ap.MasterId equals a.ID
join p in db.Posts on ap.PostId equals p.Id
select new MappingList
{
id = ap.Id,
Masterid = ap.MasterId,
postid = ap.PostId,
firstname = a.FirstName,
Postname = p.PostName
}).Where(r=>r.Masterid == id).ToList();
var grp = ListItems.GroupBy(r => r.Masterid).Select(r => new MappingList
{
Masterid = r.Key,
id = r.Key,
PostNames = string.Join(" , ", r.Select(g => g.Postname)),
firstname = ListItems.FirstOrDefault(q => q.Masterid == r.Key).firstname
}).FirstOrDefault();
string combindedString = string.Join(",", mappedName.GetAllSelctedCheckBoxes.ToArray());
MappedName mappedNames= new MappedName();
{
mappedNames.Id = grp.id.Value;
mappedNames.MasterId = grp.Masterid;
}
var m = db.Masters.Where(r => r.ID == id).Select(p=>p.FirstName);
ViewBag.MasterId = new SelectList(m, "FirstName");
ViewBag.DistrictId = new SelectList(db.DistrictMasters, "Id", "DistrictName");
if (mappedNames== null)
{
return HttpNotFound();
}
return View(mappedNames);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(MappedName mappedName)
{
string combindedString = string.Join(",", mappedName.GetAllSelctedCheckBoxes.ToArray());
List<int> check = new List<int>(combindedString.Split(',').Select(int.Parse));
if (ModelState.IsValid)
{
for (int i = 0; i < check.Count(); i++)
{
mappedName.PostId = Convert.ToInt32(check[i]);
mappedName.PostId = mappedName.PostId;
mappedName.MasterId = mappedName.MasterId;
db.MappedNames.Add(mappedName);
await db.SaveChangesAsync();
}
//db.Entry(mappedName).State = EntityState.Modified;
//await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(mappedName);
}
this is my edit action of httpget and httppost
view
@model A.Models.MappedName
<div class="form-group">
@Html.LabelFor(model => model.PostId, "PostName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div id="PostMaster"></div>
@Html.HiddenFor(m => m.GetAllSelctedCheckBoxes)
@Html.ValidationMessageFor(model => model.PostId, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
<script>
$("#Create").click(function () {
$('#GetAllSelctedCheckBoxes').empty();
debugger;
var getList = [];
$("#PostMaster input:checked").each(function () {
getList.push($(this).val());
});
$('#GetAllSelctedCheckBoxes').val(getList);// assign to hidden field
});
</script>
Here in this GetAllSelctedCheckBoxes I am getting all the selected checkbox items
model
public partial class MappedName
{
public int Id { get; set; }
public Nullable<int> MasterId { get; set; }
public Nullable<int> PostId { get; set; }
public List<string> GetAllSelctedCheckBoxes { get; set; }
}
Here the GetAllSelctedCheckBoxes is not bind to db and is written in model class My Problem is that I need to show the selected checkbox as check when I click on the edit link and need to save the edited checked box if any change occurs for eg, adding one more checkbox ? how can I do that ?can anyone please help me to find the solution ??
Aucun commentaire:
Enregistrer un commentaire