我用 javascript 创建了一个这样的函数:
function addNewManufacturer() {
var name = $("#id-manuf-name").val();
var address = $("#id-manuf-address").val();
var phone = $("#id-manuf-phone").val();
var sendInfo = {
Name: name,
Address: address,
Phone: phone
};
$.ajax({
type: "POST",
url: "/Home/Add",
dataType: "json",
success: function (msg) {
if (msg) {
alert("Somebody" + name + " was added in list !");
location.reload(true);
} else {
alert("Cannot add to list !");
}
},
data: sendInfo
});
}
我调用了 jquery.json-2.3.min.js
脚本文件,并将其用于 toJSON(array)
方法。
在控制器中,我有这个 Add
动作
[HttpPost]
public ActionResult Add(PersonSheets sendInfo) {
bool success = _addSomethingInList.AddNewSomething( sendInfo );
return this.Json( new {
msg = success
});
}
但是作为方法参数的 sendInfo
变为空。
模式:
public struct PersonSheets
{
public int Id;
public string Name;
public string Address;
public string Phone;
}
public class PersonModel
{
private List<PersonSheets> _list;
public PersonModel() {
_list= GetFakeData();
}
public bool AddNewSomething(PersonSheets info) {
if ( (info as object) == null ) {
throw new ArgumentException( "Person list cannot be empty", "info" );
}
PersonSheets item= new PersonSheets();
item.Id = GetMaximumIdValueFromList( _list) + 1;
item.Name = info.Name;
item.Address = info.Address;
item.Phone = info.Phone;
_list.Add(item);
return true;
}
}
当使用 POST 发送数据时,如何在操作方法中执行操作?
我不知道怎么用。 此外,是否可以通过 JSON 发送回应(到 ajax) ?