简体   繁体   English

如何使用MVC表单提交发布JSON数据?

[英]How can I post JSON data using mvc form submit?

I have hidden fields on my view which contains data in Json Format. 我的视图中有隐藏字段,其中包含Json格式的数据。 I want to post this data using form submit. 我想使用表单提交来发布此数据。 I can't use ajax. 我不能使用ajax。

<input type="hidden" id="workData" name="Works" data-value="[{"Id":44,"Body":"Completion Status","IsCompleted":true},{"Id":11,"Body":"Completion details","IsCompleted":false}]" value="{"Id":"33","Body":"Status","IsCompleted":true}">

<input type= "hidden" name ="Name" value="Micheal">

I have a Employee Model and a Work Model. 我有一个员工模型和一个工作模型。 Each employee is assigned list of works. 每个员工都被分配了一份工作清单。

public class Employee
{
    public string Name {get; set;}
    public List<Work> Works {get; set;} 
}

public class Work 
{
    public string Body {get; set;}
    public boll IsCompleted{get; set;}
}

My action method signature is this 我的动作方法签名是这个

public ActionResult SetWorkStatus(Employee employee)

How can I post this data to the action? 如何将这些数据发布到操作中?

I faced a similar problem when I couldn't use ajax. 当我无法使用ajax时,我遇到了类似的问题。 So this is how I managed to get it to work. 所以这就是我设法使其工作的方式。

First replace the double quotes around your data-value attribute with single quotes ' ' 首先,用单引号' '替换data-value属性周围的双引号

Then you have to append hidden inputs to your form. 然后,您必须将隐藏的输入附加到表单中。 They will then bind correctly to your model. 然后,它们将正确绑定到您的模型。

To bind complex objects, you need to provide an index for each item, rather than relying on the order of items. 要绑定复杂的对象,您需要为每个项目提供一个索引,而不是依赖于项目的顺序。 This ensures we can unambiguously match up the submitted properties with the correct object. 这确保了我们可以明确地将提交的属性与正确的对象进行匹配。

@using (Html.BeginForm("SetWorkStatus", "Controller", FormMethod.Post))
{
   @Html.HiddenFor(e => e.Name)
   <input type="hidden" id="workData" data-value='[{"id":44,"body":"completion status","iscompleted":true},{"id":11,"body":"completion details","iscompleted":false}]' value='{"id":"33","body":"status","iscompleted":true}'>
   <input type="submit" value="submit" />
}

@section scripts{
<script type="text/javascript">

    $(function () {



        $("input:submit")
        .on("click", function () {

            var str = JSON.stringify($("#workData").data("value"));
            var data = JSON.parse(str).map(function (e) {
                return { Body: e.body, IsCompleted: e.iscompleted };
            });

            data.forEach(function (e, i) {

                $("form").append("<input type='hidden' name='Employee.Works[" + i + "].Body' value='" + e.Body + "' />" +
                                 "<input type='hidden' name='Employee.Works[" + i + "].IsCompleted' value='" + e.IsCompleted + "' />");

            });

        });

    });

</script>

}

Note that the index must be an unbroken sequence of integers starting at 0 and increasing by 1 for each element. 请注意,索引必须是不间断的整数序列,从0开始并为每个元素加1。

See model binding to list 查看模型绑定到列表

The MVC model binder doesn't know how to convert the JSON value of your Work input to a model. MVC模型联编程序不知道如何将您的Work输入的JSON值转换为模型。 One thing you can try is changing your POST model to use a string for Works, and then parse the JSON in your action. 您可以尝试做的一件事是更改POST模型以将字符串用于Works,然后在操作中解析JSON。

public class PostEmployeeModel
{
    public string Name {get; set;}
    public string Works {get; set;} 
}

[HttpPost]
public ActionResult SetWorkStatus(PostEmployeeModel employee)
{
    Work work = JsonConvert.DeserializeObject<Work>(employee.Works)
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM