I have a doubt in mvc that when I click on the create new link I need to get all the value from postmaster but which is not saved in the mapping table. that is the data that are entered in table already must not visible when creating a new list. actually now the working is when I select an item from dropdownlist, the corresponding values for the item will display as checkbox , for getting work in this code , i have written it in JsonResult action method and jquery
controller
public ActionResult Create()
{
ViewBag.DistrictId = new SelectList(db.DistrictMasters, "Id", "DistrictName");
return View();
}
// Json Call to get state
public JsonResult GetPosts(string id)
{
List posts = new List();
var postList = this.Getpost(Convert.ToInt32(id));
ViewBag.Postdata = this.Getpost(Convert.ToInt32(id));
var postData = postList.Select(m => new SelectListItem()
{
Text = m.PostName,
Value = m.Id.ToString(),
});
return Json(postData, JsonRequestBehavior.AllowGet);
}
// Get State from DB by country ID
public IList Getpost(int DistrictId)
{
return db.PostMasters.Where(m => m.DistrictID == DistrictId).ToList();
}
view page of create
@Html.LabelFor(model => model.PostId,"PostName", htmlAttributes: new { @class = "control-label col-md-2" })
@Html.HiddenFor(m => m.GetAllSelctedCheckBoxes)
@Html.ValidationMessageFor(model => model.PostId, "", new { @class = "text-danger" })
$(document).ready(function () {
//District Dropdown Selectedchange event
$("#DistrictId").change(function () {
$("#PostMaster").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetPosts")', // Calling json method
dataType: 'json',
data: { id: $("#DistrictId").val() },
// Get Selected post id.
success: function (posts) {
$.each(posts, function (i, post) {
$("#PostMaster").append('' + post.Text + '
');
');
});
},
error: function (ex) {
alert('Failed to retrieve post.' + ex);
}
});
return false;
})
});
this is the script part where I am getting the value of all the postname when selecting distict. But I need only th postname that are not already saved into the table called mapping. the postnames are retrieving from postmaster table. can anyone please help me to find a solution for this that how to filter the data in controller ??
athira kPosted Apr 18, 2017, 2:28 AM
Suvendu Shekhar GiriPosted Apr 18, 2017, 2:27 AM