inforakesha

A fine WordPress.com site

How to call Controller Method and populate DropDownList

Leave a comment

 

 

STEP 1:-method in controller for populating the dropdownlist

publicActionResultstoreList(string City)
{
UserInfoService.UserInfoClientproxy=newUserInfoService.UserInfoClient();

IEnumerable<Stores>Store=proxy.GetStoreByCity(City);
ViewBag.StoreList=fromsinStore
selectnewSelectListItem{Text=s.Name,Value=Convert.ToString(s.Id)};

returnPartialView("_StoreList");

}

 

STEP 2:-this method should be called on first ddl change event in view page.

 

First ddl:

@Html.DropDownList("CityList",ViewBag.CityListasSelectList,new{id="CityList1"})

and JavaScript code to call method:-

<scripttype="text/javascript">
$(function(){
$('#CityList1').change(function(){
varc=$('#CityList1').val();
$('#partialDropDown').load('@Url.Action("storeList","User")',{city:c});
});
})

</script>

Step3:-define a div if that viewbag is null then not call the partial view else call the partial view

<divid="partialDropDown">
@if(ViewBag.StoreList!=null)
{
@Html.Partial("_StoreList")// this is the view name
}
</div>

Step:4:-define partial view

@Html.DropDownList("StoreList",ViewBag.StoreList as SelectList)

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s