相关疑难解决方法(0)

将JSON反序列化为C#动态对象?

有没有办法将JSON内容反序列化为C#4动态类型?为了使用DataContractJsonSerializer,跳过创建一堆类会很不错.

.net c# serialization json dynamic

919
推荐指数
22
解决办法
67万
查看次数

如何将JSON作为MVC 5操作方法参数接收

我整个下午都试图在网上爬行,试图在动作控制器中接收一个JSON对象.

这样做的正确和/或更简单的方法是什么?

我尝试过以下方法:1:

    //Post/ Roles/AddUser
    [HttpPost]
    public ActionResult AddUser(String model)
    {
        if(model != null)
        {
            return Json("Success");
        }else
        {
            return Json("An Error Has occoured");
        }

    }
Run Code Online (Sandbox Code Playgroud)

这给了我输入的空值.

2:

    //Post/ Roles/AddUser
    [HttpPost]
    public ActionResult AddUser(IDictionary<string, object> model)
    {
        if(model != null)
        {
            return Json("Success");
        }else
        {
            return Json("An Error Has occoured");
        }

    }
Run Code Online (Sandbox Code Playgroud)

这给了我jquery方面的500错误,试图发布到它?(意思是它没有正确绑定).

这是我的jQuery代码:

<script>
function submitForm() {

    var usersRoles = new Array;
    jQuery("#dualSelectRoles2 option").each(function () {
        usersRoles.push(jQuery(this).val());
    });
    console.log(usersRoles);

    jQuery.ajax({
        type: "POST",
        url: "@Url.Action("AddUser")",
        contentType: "application/json; charset=utf-8",
        dataType: "json", …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc json actionmethod

65
推荐指数
2
解决办法
19万
查看次数

将JSON对象发送到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 …
Run Code Online (Sandbox Code Playgroud)

jquery c#-4.0 asp.net-mvc-4 asp.net-web-api

22
推荐指数
2
解决办法
10万
查看次数