将json发布到MVC控制器

时间:2014-12-15 23:19:45

标签: c# ajax json model-view-controller

我构建了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

Csharp Class:

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

1 个答案:

答案 0 :(得分:0)

这是我如何将课程传递给我的控制器功能的一个问题。

这就是我修复它的方法。

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

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

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)我将步骤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