使用 json POST 到 MVC 控制器

本文关键字:MVC 控制器 POST json 使用 | 更新日期: 2023-09-27 18:34:16

我构建了CSharp类Visual Studios"Paste Special" --> Json到Classes。我会发布jsonData,但它相当大,不想浪费你的时间。我猜我的cihForm.cs需要一个构造函数类,但这似乎不是使用数据的正确方法。

问题似乎是cihForm类没有正确使用 json 数据。我认为这是因为cihForm结构。

查看 Ajax 调用:

var jsonData = JSON.stringify(data);
$.ajax({
    url: '/Forms/Create_Post',
    type: 'POST',
    dataType: 'json',
    data: jsonData,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        // get the result and do some magic with it
        var message = data.Message;
        console.log(message);
    }
});

控制器操作:

<HttpPost>
<ActionName("Create")>
Function Create_Post(jsonData As Rootobject) As JsonResult
  //jsonData seems to be blank
  // immediate window debug below
  //?jsonData
  //{CIH.Library.CSharp.cihForm}
End Function

夏普级:

namespace CIH.Library.CSharp
{
        public class Rootobject
        {
            public Theform theForm { get; set; }
        }
        public class Theform
        {
            public Formoption[] formOptions { get; set; }
            public Formnotification[] formNotifications { get; set; }
            public Field[] fields { get; set; }
        }
        public class Formoption
        {
            public string reqApproval { get; set; }
            public string orderedApproval { get; set; }
            public string[] approvalStages { get; set; }
            public string reqLogin { get; set; }
            public Limitsubmission[] limitSubmissions { get; set; }
            public Formexpire[] formExpires { get; set; }
            public string attachForm { get; set; }
            public string toOrganizations { get; set; }
            public string[] orgsToAttach { get; set; }
            public string toEvents { get; set; }
            public string[] eventsToAttach { get; set; }
            public string showInAdminForms { get; set; }
            public string useAsSurvey { get; set; }
        }
        public class Limitsubmission
        {
            public string responsesPerUser { get; set; }
            public string responsesPerForm { get; set; }
        }
        public class Formexpire
        {
            public string startTime { get; set; }
            public string endTime { get; set; }
        }
        public class Formnotification
        {
            public string showSuccessMessage { get; set; }
            public string successMessage { get; set; }
            public string redirectOnSuccess { get; set; }
            public string redirectUrl { get; set; }
            public string adminNotification { get; set; }
            public string notifyAdminTo { get; set; }
            public string notifyAdminSubject { get; set; }
            public string notifyAdminbody { get; set; }
            public string submitterNotification { get; set; }
            public string submitterSubject { get; set; }
            public string submitterBody { get; set; }
        }
        public class Field
        {
            public string fieldTypeId { get; set; }
            public string title { get; set; }
            public string description { get; set; }
            public string sortOrder { get; set; }
            public string isReq { get; set; }
            public string allowMultiple { get; set; }
            public string[] dropOptions { get; set; }
            public string[] listOptions { get; set; }
            public string scaleMin { get; set; }
            public string scaleMax { get; set; }
            public string allowPast { get; set; }
            public string limitToEmailDomain { get; set; }
            public string emailDomainToLimitTo { get; set; }
            public string limitFileTypes { get; set; }
            public string[] listOfLimitedFileTypes { get; set; }
            public string limitAccess { get; set; }
            public string limitToCategory { get; set; }
            public string[] limitedCategories { get; set; }
            public string limitToOrganization { get; set; }
            public string limitedOrganization { get; set; }
            public string infoTextBody { get; set; }
            public string imageCaption { get; set; }
            public string imageURL { get; set; }
        }        
}

使用 json POST 到 MVC 控制器

这是我如何将类传递到控制器函数中的问题。

这就是我修复它的方式。

1) 使用本站创建JSON模板 -- http://www.objgen.com/json

2) 使用此网站创建 C# 类 -- http://json2csharp.com/

3) 在视图页面上创建<script>标记,该标记使用 JSON 数据执行 ajax 调用

   <button type="button" onclick="submitForm()">Save</button>
    <script type="text/javascript">
        function submitForm() {
            var data = //paste JSON template created in step #1 here
            var jsonData = JSON.stringify(data);
        $.ajax({
            url: '/Forms/Create_Post',
            type: 'POST',
            dataType: 'json',
            data: jsonData,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                // get the result and do some magic with it
                var message = data.Message;
                console.log(message);
            }
        });
    </script>

3)我将步骤2中创建的类粘贴到类文件中。

4)在我的控制器中,我随后传入了RootObject

   <HttpPost>
    <ActionName("Create")>
    Function Create_Post(jsonData As RootObject) As ACtionResult
        Dim json As New RootObject()
        json = JsonConvert.DeserializeObject(Of RootObject)(jsonData.ToString())

    End Function