I want to Bind Dropdown selected countryId to my class CountryId and Selected Stateid Value to StateId using angularjs.
My Country and State Dropdown has been loaded successfully.
This is my Model:
public partial class Friends
{
public int Id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
public string postalcode { get; set; }
public string notes { get; set; }
public Nullable<int> CountryId { get; set; }
public Nullable<int> StateId { get; set; }
}
This is my View:
<select data-ng-model="newfriend.country2" data-ng-options="c as c.Name for c in allItems" data-ng-change="">
<option value="">-- Select Country --</option>
</select>
<label>State:</label>
<select data-ng-model="newfriend.state2" data-ng-disabled="!newfriend.country2" data-ng-options="s.Id as s.Name for s in newfriend.country2.State">
<option value="">-- Select State --</option>
</select>
This is my controller method to get both Country State in one method:
public JsonResult GetCountryState()
{
var Data = db.Country.Select
(
d => new CountryModel
{
Id = d.Id,
Name = d.Name,
State = d.State.Select
(
x => new StateModel
{
Id = x.Id,
Name = x.Name
}
).ToList()
}
).ToList();
return Json(Data, JsonRequestBehavior.AllowGet);
}
This is my call to Add Controller method:
$http.post('../Home/AddFriends', this.newfriend).success(function (data) {}
My Controller Method in which i want to bind my values to FriendsModel):
public ActionResult AddFriends(Friends FriendsModel)
{
}
Can anybody help me???