如何下拉列表中选择的值绑定到我的angularjs模型 [英] How to bind Selected Value of dropdown to my model with angularjs
问题描述
我要选择下拉到countryId我班CountryId及选定STATEID值使用angularjs到STATEID进行绑定。
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.
这是我的模型:
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; }
}
这是我的看法:
<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) {}
我控制器的方法中,我想我的值绑定到FriendsModel):
My Controller Method in which i want to bind my values to FriendsModel):
public ActionResult AddFriends(Friends FriendsModel)
{
}
任何人可以帮助我吗?
Can anybody help me???
推荐答案
我有一个创建了一个基本plunker
I have a created a basic plunker
http://plnkr.co/edit/Wylq6UOuh7XBV7aX7OAL?p=$p$ PVIEW
$scope.states=[{id:1,name:'state1'}, {id:2,name:'state2'}, {id:3,name:'state3'}, {id:4,name:'state4'}]
<select data-ng-model="selectedState">
<option data-ng-repeat="state in states" value="{{state.id}}">{{state.name}}</option>
</select>
在初始化中selectedState控制器FO例如: $ scope.selectedState = 1;
这将使STATE1为默认选中的选项
Initialise the selectedState in your controller fo eg: $scope.selectedState =1;
this will make state1 as default selected option.
这篇关于如何下拉列表中选择的值绑定到我的angularjs模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!