Hi,
Based on previous answer in Populate TextBoxes on DropDownList change using jQuery AJAX in ASP.Net MVC
I want to update the value of checkbox from false to true.
I did try the code but it save the data multiple times and data become redundant.
After submit the form the data will be display in the table which table only display TRUE checkbox only.
This is my controller to save the checkbox.
public ActionResult Save(tblPlatformList platformlist)
{
var a = db.tblPlatformLists.Where(x => x.iSelected == true).ToList();
foreach (var item in a)
{
//Update the tblplatformlist
tblPlatformList model = db.tblPlatformLists.Where(x => x.list_id == item.list_id).FirstOrDefault();
platformlist.iSelected = true;
db.tblPlatformLists.Add(platformlist);
db.SaveChanges();
}
return RedirectToAction("ViewList");
}
This is my View.
@model dashboard.Models.tblPlatformList
@using dashboard.Models;
@{
testGEntities db = new testEntities();
WebGrid webGrid = new WebGrid(source: db.tblPlatformLists.Where(x => x.iSelected == true));
}
@section Scripts
{
<!-- Multiple Table Display -->
<script type="text/javascript" class="init">
$(document).ready(function () {
$('#ItemList').DataTable({
//"order": [[7, "asc"], [3, "desc"]],
//"lengthMenu": [[20, 40, 80, -1], [20, 40, 80, "All"]]
});
});
</script>
<script type="text/javascript">
function get_sbu() {
var id = $('#ddlCustomers').find(":selected").attr('value');
$.ajax({
type: "POST",
url: "/Home/GetData",
data: "id=" + id,
success: function (data) {
if (data.length > 0) {
$('#task').val(data[0].pNum);
$('#task2').val(data[0].bType);
}
}
});
}
</script>
}
<br /><br />
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-inline">
<div class="form-group">
<label for="st" class="mr-sm-2">Platform:</label>
<select id="ddlCustomers" onchange="get_sbu()">
<option value="0">Select</option>
@foreach (var item in (List<SelectListItem>)TempData["platform"])
{
<option value="@item.Text">@item.Value</option>
}
</select>
</div>
<div class="form-group">
<label for="st" class="mr-sm-2">PH Number:</label>
@Html.TextBoxFor(m => m.pNum, new { @readonly = "readonly", id = "task" })
</div>
<label for="st" class="mr-sm-2">Base Type:</label>
@Html.TextBoxFor(m => m.bType, new { @readonly = "readonly", id = "task2" })
<label for="st" class="mr-sm-2">Email Required?:</label>
@Html.CheckBoxFor(model => model.iSelected)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
</div>
}
<br />
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "table table-striped table-hover" },
columns: webGrid.Columns(
webGrid.Column("No.", format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(webGrid.TotalRowCount / webGrid.PageCount) / webGrid.RowsPerPage) * webGrid.RowsPerPage * webGrid.PageIndex),
webGrid.Column("pName", "Platform Name"),
webGrid.Column("pNum", "PH Number"),
webGrid.Column("bType", "Base Type"),
webGrid.Column(null, null, format: @<text><input type="checkbox" name="iSelected" value="@item.iSelected" checked="checked" /></text>)))