如何将动态列表发布到asp mvc控制器

时间:2016-05-07 13:18:37

标签: asp.net angularjs asp.net-mvc

我有这样的模特:

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)
{
    // ...
}

提前致谢

1 个答案:

答案 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是具有keyvalue属性的对象数组。例如:

$scope.items = [
    { key: 'key1', value: 'value1' },
    { key: 'key2', value: 'value2' },
    { key: 'key3', value: 'value3' }
];