【问题标题】:Consume json POSTed to MVC controller使用 POST 到 MVC 控制器的 json
【发布时间】:2014-12-15 23:19:44
【问题描述】:

我构建了 CSharp 类 Visual Studios "Paste Special" --> Json to 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 类:

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

【问题讨论】:

  • {CIH.Library.CSharp.cihForm}(从您的即时窗口)意味着jsonData 不是 空白(至少不是null)。 jsonData 变量中究竟缺少什么?
  • 好吧,我似乎无法使用数据。当我尝试使用?jsonData.Theform 深入课堂时,它会返回Theform is a type and cannot be used as an expression
  • 你试过小写吗? theForm?
  • 是的,我已经尝试了所有的案例。我的视图有@ModelType CIH.Library.CSharp.Rootobject 作为页面的模型。这有关系吗?

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


【解决方案1】:

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

这就是我修复它的方法。

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

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

3) 在视图页面上创建&lt;script&gt; 标记,使用 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

【讨论】:

    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2013-04-20
    • 2012-06-20
    • 2016-11-26
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多