I created a function in javascript like that:
(我在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
});
}
I called jquery.json-2.3.min.js
script file and I used it for toJSON(array)
method.
(我调用了jquery.json-2.3.min.js
脚本文件,并将其用于toJSON(array)
方法。)
In controller, I have this Add
action
(在控制器中,我有此Add
操作)
[HttpPost]
public ActionResult Add(PersonSheets sendInfo) {
bool success = _addSomethingInList.AddNewSomething( sendInfo );
return this.Json( new {
msg = success
});
}
But sendInfo
as method parameter becomes null.
(但是sendInfo
作为方法参数变为空。)
The model:
(该模型:)
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;
}
}
How could I do in action method when the data was sent with POST ?
(用POST发送数据时,我该如何采取行动方法?)
I don't know how to use.
(我不知道该怎么用。)
Also, it is possible to send back the response (to ajax) via JSON ? (另外,可以通过JSON将响应发送回(到ajax)吗?)
ask by Snake Eyes translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…