将JSON对象发送到Web API

Cor*_*ory 22 jquery c#-4.0 asp.net-mvc-4 asp.net-web-api

我试图找出如何从表单向Web API操作发送一些信息.这是我试图使用的jQuery/AJAX:

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 });
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是我的模特

public class PartSourceModel
{
    public int ID { get; set; }
    public int ProductID { get; set; }
    public int VendorID { get; set; }
    public string PartNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的看法

<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" />
Run Code Online (Sandbox Code Playgroud)

这是我的控制器动作

// POST api/partsourceapi
public void Post(PartSourceModel model)
{
    // currently no values are being passed into model param
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?现在,当我调试并在ajax请求命中控制器操作时逐步执行此操作时,没有任何内容传递给模型参数.

jzm*_*jzm 29

试试这个:

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 });
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)

调节器

    public string Post(PartSourceModel model)
    {
        return model.PartNumber;
    }
Run Code Online (Sandbox Code Playgroud)

视图

<label>Part Number</label>
<input type="text" id="part-number" name="part-number" />

<input type="submit" id="save-source" name="save-source" value="Add" />
Run Code Online (Sandbox Code Playgroud)

现在,当您Add在填写文本框后单击" " 时,controllerPartNumber在警报框中吐出您在框中编写的内容.


Jer*_*oen 6

更改:

 data: JSON.stringify({ model: source })
Run Code Online (Sandbox Code Playgroud)

至:

 data: {model: JSON.stringify(source)}
Run Code Online (Sandbox Code Playgroud)

在你的控制器中你这样做:

public void PartSourceAPI(string model)
{
       System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

   var result = js.Deserialize<PartSourceModel>(model);
}
Run Code Online (Sandbox Code Playgroud)

如果你在jquery中使用的url是/api/PartSourceAPI控制器名称必须是api,并且action(方法)应该是PartSourceAPI