我试图将我的模型列表发送到Controller中的方法,如下所示:
import {Component} from '@angular/core';
import {NgGrid, NgGridItem} from 'angular2-grid';
@Component({
selector: 'my-app',
template: `
<h4>My First Angular 2 App</h4>
<div class="grid" [ngGrid]="{\'max_cols\': 6,\'max_rows\': 4, \'auto_resize\': true}">
<div class="grid-item" [ngGridItem]="{\'sizex\': 3, \'sizey\': 2}">
<input type="text" [(ngModel)] = 'pageTitle'/>
</div>
<div class="grid-item" [ngGridItem]="{\'sizex\': 1, \'sizey\': 1}">
</div>
<div class="grid-item" [ngGridItem]="{\'sizex\': 1, \'sizey\': 1}">
<input type="text" [(ngModel)] = 'pageTitle' />
<button type="button" (click)="selectGender()">Select Gender</button>
</div>
</div>
`,
directives: [NgGrid, NgGridItem]
})
Fiddler中的Ajax输出:
$("table#phonelist tr").each(function () {
$this = $(this)
var phonetype = $this.find("#phonetupeselect option:selected").val();
var phonenumber = $this.find("#phonenumber").val();
var obj = new Object();
if (phonetype !== undefined && phonenumber !== undefined && phonenumber !== "")
{
obj.ID = 0;
obj.Number = phonenumber;
obj.PhoneTypeId = phonetype;
phonelist.push(obj);
}
});
var emps = JSON.stringify({ 'phonelist': phonelist });
$.ajax({
contentType: 'application/json; charset=utf-8',
type: 'POST',
url: "/Controller1/AddPhone",
data: emps ,
success: function (result) {
submit();
},
});
Controller1中的方法:
{"phonelist":[{"ID":0,"Number":"111111","PhoneTypeId":"10"},{"ID":0,"Number":"222222","PhoneTypeId":"11"},{"ID":0,"Number":"333333","PhoneTypeId":"12"}]}
PhoneViewModel型号:
[HttpPost]
public void AddPhone(List<PhoneViewModel> phonelist)
{
//empty atm
}
无法找到为什么phonelist在控制器方法中总是为空。
答案 0 :(得分:0)
代码略有变化。这将解决。
var emps = JSON.stringify({ 'phonelist': phonelist });
var model = { phonelist: JSON.parse(phonelist) };
$.ajax({
contentType: 'application/json',
type: 'POST',
url: "/Controller1/AddPhone",
data: JSON.stringify(model),
success: function (result) {
submit();
},
});