问题描述
我想弄清楚如何将一些信息从表单发送到 Web API 操作.这是我尝试使用的 jQuery/AJAX:
var source = {'ID': 0,'ProductID': $('#ID').val(),'PartNumber': $('#part-number').val(),'供应商ID':$('#Vendors').val()}$.ajax({类型:POST",数据类型:json",url: "/api/PartSourceAPI/",数据:JSON.stringify({模型:源}),成功:功能(数据){alert('成功');},错误:函数(错误){jsonValue = jQuery.parseJSON(error.responseText);jError('保存新零件源时出错:' + jsonValue, { TimeShown: 3000 });}});
这是我的模型
公共类 PartSourceModel{公共 int ID { 获取;放;}公共 int ProductID { 获取;放;}公共 int VendorID { 获取;放;}公共字符串 PartNumber { 获取;放;}}
这是我的观点
@foreach(Model.Sources 中的 SmallHorse.ProductSource 源代码){@source.ItemNumber
}
<label>部件号</label><input type="text" id="part-number" name="part-number"/><input type="submit" id="save-source" name="save-source" value="Add"/>
这是我的控制器动作
//POST api/partsourceapipublic void Post(PartSourceModel 模型){//当前没有值被传递到模型参数}
我错过了什么?现在,当我在 ajax 请求命中控制器操作时调试并逐步执行此操作时,没有任何内容被传递到模型参数中.
试试这个:
jquery
$('#save-source').click(function (e) {e.preventDefault();变量源 = {'ID': 0,//'ProductID': $('#ID').val(),'PartNumber': $('#part-number').val(),//'VendorID': $('#Vendors').val()}$.ajax({类型:POST",数据类型:json",url: "/api/PartSourceAPI",数据源,成功:功能(数据){警报(数据);},错误:函数(错误){jsonValue = jQuery.parseJSON(error.responseText);//jError('保存新部分源时出错:' + jsonValue, { TimeShown: 3000 });}});});
控制器
public string Post(PartSourceModel 模型){返回模型.零件编号;}
查看
<input type="text" id="part-number" name="part-number"/><input type="submit" id="save-source" name="save-source" value="Add"/>
现在,当您在填写完文本框后单击Add
"时,controller
会吐出您在PartNumber
警报框.
I am trying to figure out how I can send some information from a form to a Web API action. This is the jQuery/AJAX I'm trying to use:
var source = {
'ID': 0,
'ProductID': $('#ID').val(),
'PartNumber': $('#part-number').val(),
'VendorID': $('#Vendors').val()
}
$.ajax({
type: "POST",
dataType: "json",
url: "/api/PartSourceAPI/",
data: JSON.stringify({ model: source }),
success: function (data) {
alert('success');
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
Here is my model
public class PartSourceModel
{
public int ID { get; set; }
public int ProductID { get; set; }
public int VendorID { get; set; }
public string PartNumber { get; set; }
}
Here is my view
<div id="part-sources">
@foreach (SmallHorse.ProductSource source in Model.Sources)
{
@source.ItemNumber <br />
}
</div>
<label>Part Number</label>
<input type="text" id="part-number" name="part-number" />
<input type="submit" id="save-source" name="save-source" value="Add" />
Here is my controller action
// POST api/partsourceapi
public void Post(PartSourceModel model)
{
// currently no values are being passed into model param
}
What am I missing? right now when I debug and step through this when the ajax request hits the controller action there is nothing being passed into the model param.
Try this:
jquery
$('#save-source').click(function (e) {
e.preventDefault();
var source = {
'ID': 0,
//'ProductID': $('#ID').val(),
'PartNumber': $('#part-number').val(),
//'VendorID': $('#Vendors').val()
}
$.ajax({
type: "POST",
dataType: "json",
url: "/api/PartSourceAPI",
data: source,
success: function (data) {
alert(data);
},
error: function (error) {
jsonValue = jQuery.parseJSON(error.responseText);
//jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
});
Controller
public string Post(PartSourceModel model)
{
return model.PartNumber;
}
View
<label>Part Number</label>
<input type="text" id="part-number" name="part-number" />
<input type="submit" id="save-source" name="save-source" value="Add" />
Now when you click 'Add
' after you fill out the text box, the controller
will spit back out what you wrote in the PartNumber
box in an alert.
这篇关于将 JSON 对象发送到 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!