简体   繁体   English

消费JSON POST到MVC控制器

[英]Consume json POSTed to MVC controller

I built the CSharp class Visual Studios "Paste Special" --> Json to Classes. 我建立了CSharp类Visual Studios“ Paste Special”-> Json to Classes。 I would post the jsonData but it is fairly large and dont want to waste your time. 我会发布jsonData,但它相当大,不想浪费您的时间。 I'm guessing my cihForm.cs needs a constructor class but that didn't seem like the right way to go about consuming the data. 我猜我的cihForm.cs需要一个构造函数类,但这似乎不是使用数据的正确方法。

The problem seems to be that the cihForm class is not consuming the json data correctly. 问题似乎是cihForm类未正确使用json数据。 I assume it is because of the cihForm structure. 我认为这是因为cihForm结构。

View Ajax call: 查看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);
    }
});

Controller Action: 控制器动作:

<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

Csharp Class: Csharp类别:

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; }
        }        
}

It was an issue with how i was passing the class into my controller function. 我如何将类传递到控制器函数中是一个问题。

This is how i went about fixing it. 这就是我修复它的方式。

1) Create JSON template using this website -- http://www.objgen.com/json 1)使用此网站创建JSON模板-http: //www.objgen.com/json

2) Create C# classes using this website -- http://json2csharp.com/ 2)使用此网站创建C#类-http://json2csharp.com/

3) Create <script> tag on view page that does ajax call with json data 3)在使用json数据进行ajax调用的视图页面上创建<script>标签

   <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) I pasted the classes created in step 2 into a class file. 3)我将第2步中创建的类粘贴到类文件中。

4) in my controller i then passed in RootObject 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

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

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