athira k

athira k

  • NA
  • 140
  • 53.5k

get data which is not stored in db for creating new entry

Apr 18 2017 2:24 AM
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<SelectListItem> posts = new List<SelectListItem>();
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<PostMaster> Getpost(int DistrictId)
{
return db.PostMasters.Where(m => m.DistrictID == DistrictId).ToList();
}
 
 
view page of create
 
<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>
</div>
 
 
<script type="text/javascript">
$(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('<input type="checkbox" name="postdata" value=' + post.Value + '>' + post.Text + '<br>');
});
},
error: function (ex) {
alert('Failed to retrieve post.' + ex);
}
});
return false;
})
});
</script>
 
 
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 ?? 

Answers (2)