【发布时间】: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