Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

CAn you please help me on this.

i have created asp.net mvc3 application i want binding the values from DB to dropdownlist and need to selected item pass to respective controller how i can do please help me and also error messagebox need to create please help mke on this.

Advanced Thankxs

Regards,

Narasimha
Posted

The link below is all you need! Start to finish. Not sure what you mean by the last part of your question (error messagebox).

http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc[^]

Hope this helps!
 
Share this answer
 
use jquery ajax and return string - if string is blank no error else show alert with message returned from controller.
http://api.jquery.com/jQuery.ajax/[^]
 
Share this answer
 
Check this one

View Part :-
JavaScript
$("#istateid").change(function () {
 $.ajax({
   type: "POST",
   url: "/PostAd/getCityJson",
   data: { stateId: $("#istateid > option:selected").attr("value") },
   success: function (data) {
       var items = [];
       items.push("<option>--Choose Your Area--</option>");
       $.each(data, function () {
           items.push("<option value=" + this.Value + ">" + this.Text + "</option>");
       });
       $("#icityid").html(items.join(' '));
   }
 })
});


Controller Part -
C#
[HttpPost]
public JsonResult getCityJson(string stateId, string selectCityId=null)
{
    return Json(getCity(stateId, selectCityId));
}
public SelectList getCity(string stateId, string selectCityId = null)
{
    IEnumerable<SelectListItem> cityList = new List<SelectListItem>();
    if (!string.IsNullOrEmpty(stateId))
    {
        int _stateId = Convert.ToInt32(stateId);
        cityList = (from m in _db.mstrcities where m.bstatus == true && m.istateid == _stateId select m).AsEnumerable().Select(m => new SelectListItem() { Text = m.vcity, Value = m.icityid.ToString() });
    }
    return new SelectList(cityList, "Value", "Text", selectCityId);

}


View more about it how to bind through viewbag / model / jquery, more example -

binding dropdownlist in .net mvc razor by viewbag, model and jquery selected index change - three ways
 
Share this answer
 
v3

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