Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I was trying to filter data on html table based on the selection changed in drop down list. I was able to show data in page in a and also able to pass the drop down value. But not quite sure how to rebind that
data.Could someone please help will be highly appreciated.

Thanks,
Palash
Posted
Comments
Nathan Minier 12-Sep-14 9:06am    
Are you using jQuery or any other JavaScript frameworks?
Sanket Saxena 12-Sep-14 9:07am    
Share your code

1 solution

I am new in JQuery Ajax. I am not sure how to rebind data table when drop down selection changed. Can I bind on same view? Also partial rendering I tried, which I am not clear understanding, not working. Please advise if you have any or if you have any sample code would be highly appreciated.

Brief of bellow code:
-------------------
I bind drop down and view in Index() method. DDL is getting from enum class model and I build a dll Business that supplies List<person> that I bind on table data. I tried rebinding on UpdateIndex(). I am able to pass ddl selection through parameter "race" but linq is not working.

Controller Code:
===============
public ActionResult Index()
{
List<SelectListItem> selectListItem = new List<SelectListItem>();
DropDownModel dropDownModel = new DropDownModel();

dropDownModel.id = (int)enumRace.eRace.Angles;
dropDownModel.Value = enumRace.eRace.Angles.ToString();

selectListItem.Add(new SelectListItem() { Value = dropDownModel.Value, Text = dropDownModel.id.ToString() });

dropDownModel.id = (int)enumRace.eRace.Saxons;
dropDownModel.Value = enumRace.eRace.Saxons.ToString();

selectListItem.Add(new SelectListItem() { Value = dropDownModel.Value, Text = dropDownModel.id.ToString() });

dropDownModel.id = (int)enumRace.eRace.Jutes;
dropDownModel.Value = enumRace.eRace.Jutes.ToString();

selectListItem.Add(new SelectListItem() { Value = dropDownModel.Value, Text = dropDownModel.id.ToString() });

dropDownModel.id = (int)enumRace.eRace.Asians;
dropDownModel.Value = enumRace.eRace.Asians.ToString();

selectListItem.Add(new SelectListItem() { Value = dropDownModel.Value, Text = dropDownModel.id.ToString() });

ViewBag.DropDownList = new SelectList(selectListItem, "Text", "Value");

RaceModel rcModel = new RaceModel();
List<businessobject.person> people = rcModel.personCollection;

ViewBag.Peoples = people.Where(a => a.Age % 2 == 0).OrderBy(o => o.Age).ToList();

return View();
}

public ActionResult UpdateIndex(string race)
{
RaceModel rcModel = new RaceModel();
ViewBag.Peoples = null;
List<businessobject.person> people = rcModel.personCollection;
ViewBag.Peoples = people.Where(a => a.Age.ToString() == race).ToList();
return View("Race");

}

}


==================================================

View Code:
===========

@model RaceDB.Models.RaceModel

@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#RaceDropDownList").change(function () {
$.ajax({
url: "/Race/UpdateIndex",
type: 'POST',
data: { race: $(this).val() },
datatype: 'json',
success: function (data) {
var elements = "";
$.each(data, function () {
elements = elements + '<option values="' + this.id + '">' + this.state + '</option>'
})
$('#State').empty().attr('disabled', false).append(elements);
}
});
});
});
</script>


Index





@Html.DropDownList("RaceDropDownList", (SelectList)ViewBag.DropDownList);




Race Details

@foreach (var p in (ViewBag.Peoples as List<businessobject.person>))
{
}
Name Age Height
@p.Name @p.Age @p.Height @p.Race @p.plusOneYearAge

 
Share this answer
 
Comments
Thanks7872 8-Jan-15 5:59am    
Remove this and use improve question link (Bottom of the question) to show your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900