2017-07-19 92 views
0

我使用MVC ASP。爲什麼當我打電話時我的操作方法沒有觸發?

這裏是JavaScript對象,我發送到動作方法:

var layerProp = {VectorLayerId:5, FieldName: "someText", FieldType:"someText", FieldValue: "someText", Required:false } 

這裏是我的ajax功能:

function saveProperty(layerProp, callback, error) { 
    return $.ajax({ 
     url: '/Mobile/LayerProperty/SaveProperty', 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: { layerProp: layerProp }, 
     success: callback, 
     error: error 
    }); 
} 

這裏是控制我的行動類型:

[HttpPost] 
    public JsonResult SaveProperty(GeomindMobile.Core.Data.VectorLayers_Fields layerProp) 
    { 
     try 
     { 
      var q = _repository.SaveProperty(layerProp); 
      return Json(new { Result = "OK" }, JsonRequestBehavior.DenyGet); 
     } 
     catch (Exception ex) 
     { 
      return Json(new { Result = "Bad", ex.Message }); 
     } 
    } 

這是我的實體,類型defenition:

public partial class VectorLayers_Fields 
{ 
    public VectorLayers_Fields() 
    { 
     this.VectorLayer_FieldsValue = new HashSet<VectorLayer_FieldsValue>(); 
    } 

    public int Id { get; set; } 
    public int VectorLayerId { get; set; } 
    public string FieldName { get; set; } 
    public string FieldType { get; set; } 
    public string FieldValue { get; set; } 
    public bool Required { get; set; } 

    public virtual ICollection<VectorLayer_FieldsValue> VectorLayer_FieldsValue { get; set; } 
} 

任何想法,爲什麼當我後我的數據到控制器我得到這個錯誤:

無法加載資源:服務器迴應500(內部服務器錯誤)的狀態

UPDATE:

這裏是massege有關錯誤,我在拉琴得到:

[ArgumentException: Invalid JSON primitive: layerProp.] 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +707 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +333 
    System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +110 
    System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +46 
    System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +230 
    System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +24 
    System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +45 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +223 
    System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +263 
    System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +504 
    System.Linq.Enumerable.ToList(IEnumerable`1 source) +70 
    System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +361 
    System.Web.Mvc.ControllerBase.get_ValueProvider() +50 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +77 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +143 
    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +744 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +380 
    System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +44 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164 
    System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +532 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164 
    System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +416 
    System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +120 
    System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +164 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +309 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1028 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +134 
+0

你看過什麼異常堆棧跟蹤是服務器端? 您是否嘗試在操作方法中放置斷點以查看它是否命中? –

+0

什麼是500的描述? – Ssheverdin

+0

@MartinBlore是的,我試過了,沒有達到行動方法 – Michael

回答

1

在您的JS AJAX調用,試着從改變數據屬性:

data: { layerProp: layerProp }, 

要:

data: JSON.stringify(layerProp), 

你正在做的錯誤是,你提供的數據與屬性的對象「layerProp」。因此,如果我們寫了一個C#類,你會得到這樣的:

class Whatever 
{ 
    public SomeType LayerProp { get; set; } 
} 

你控制器上的期待類型是所有你的價值觀像字段名,等的FieldType對象所以JSON格式,你的JSON對象(第一個{)的開始應該包含這些屬性。如:

data: { 'Id': 10, FieldName: 'Hello', FieldType: 'World'.... 

的JSON.stringify是在安全值轉換/符號裏面JS對象爲正確的JSON格式的所有瀏覽器的輔助功能。例如,它將使用JS Date對象並將其轉換爲帶有時區信息的有效日期/時間字符串。當試圖從JS對象中創建JSON字符串時使用它更簡單/更安全。更多的信息在這裏:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

我希望這可以幫助。

+0

感謝它的工作。你可以請求簡要解釋嗎? – Michael

+0

@Michael更新了答案:) –

相關問題