我有这样的模特:
public class Feature
{
public int id { get; set; }
public List<keyFeatures> features { get; set; }
}
public class keyFeatures
{
public string key { get; set; }
public string value { get; set; }
}
在Front中,我使用angular
动态创建密钥对值 <div ng-repeat="item in items" class="row">
<div name="features" class="row">
<div class="col-md-6">
<input type="text" class="form-control input-md" placeholder="{{item.key}}" name="key" ng-model="item.key" />
<span class="error" ng-show="$parent.submitted&& innerForm.fieldU.$error.required">Required!</span>
</div>
<div class="col-md-6">
<input type="text" class="form-control input-md" name="value" placeholder="enter text..." ng-model="item.value" />
<span class="error" ng-show="$parent.submitted && innerForm.userName.$error.required"> Required! </span>
</div>
</div>
</div>
Iv尝试了不同的方法,但它总是在post(Razor)上向我的控制器返回null
public ActionResult Create( Feature model)
{
// ...
}
提前致谢
答案 0 :(得分:0)
您可以使用Angular $http
服务来对MVC控制器进行异步调用并提交所需的数据。
var feature = {
id: 123,
features: $scope.items
};
$http.post(url, JSON.stringify(feature)).success(function(result) {
alert('successfully sent to server');
}).error(function() {
alert('an error occurred');
});
在此示例中,我假设$scope.items
是具有key
和value
属性的对象数组。例如:
$scope.items = [
{ key: 'key1', value: 'value1' },
{ key: 'key2', value: 'value2' },
{ key: 'key3', value: 'value3' }
];