2013-02-13 29 views
4

我正在使用ASP.NET MVC和jQuery通過AJAX調用保存一些數據。我目前傳遞使用jQuery AJAX()函數一些JSON數據,像這樣將JSON數據傳遞到控制器方法而無需聲明對象

$.ajax({ 
    dataType: 'json', 
    type: 'POST', 
    url: '@Url.Action("UpdateName", "Edit")', 
    data: { 
     id: 16, 
     name: 'Johnny C. Bad' 
    } 
}); 

使用這種控制器的方法和輔助類。

public void UpdateName(Poco poco) 
{ 
    var person = PersonController.GetPerson(poco.Id); 
    person.Name = poco.Name; 
    PersonController.UpdatePerson(person); 
} 

public class Poco 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

接受JSON數據的另一種方法是簡單地使用多個參數這樣

public void UpdateName(int id, string name) 
{ 
    var person = PersonController.GetPerson(id);  
    person.Name = name; 
    PersonController.UpdatePerson(person); 
} 

這種方法是確定的參數,以更小的數字,但我的現實世界中的代碼通常已經有大約5 - 10個參數。使用對象而不必聲明和使用所有這些參數非常方便。

我wondernig如果有接受JSON數據作爲一個單一的對象和不具有聲明一個類對於每個控制器的方法我想使用這種方法的另一種方式。例如,這樣的事情:

public void UpdateName(dynamic someData) 
{ 
    var person = PersonController.GetPerson(someData.Id); 
    person.Name = someData.Name; 
    PersonController.UpdatePerson(person); 
} 
+0

可以只要將它們轉換爲JSon,肯定會發布覆雜的對象。你會在jscript或Razor變量中構建複雜的對象嗎? – 2013-02-13 16:10:00

+0

另一種選擇是將json數據作爲一個字符串並在動作中反序列化。 – 2013-02-13 16:11:35

+0

http://stackoverflow.com/questions/11608026/asp-net-mvc-can-json-object-be-passed-to-a-controller-with-parameter-with-dynami – 2013-02-13 16:12:13

回答

1

可能的解決方法

創建特定的模型綁定,例如



    public class DynamicModelBinder : DefaultModelBinder 
    { 
     public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
     { 
      if (controllerContext.HttpContext.Request.Form.AllKeys.Any(x => x == "dynamic")) 
      { 
       dynamic model = new ExpandoObject(); 
       IDictionary underlyingmodel = model; 

       foreach (var key in controllerContext.HttpContext.Request.Form.AllKeys) 
        underlyingmodel.Add(key, (bindingContext.ValueProvider.GetValue(key).RawValue as string[]).First()); 

       return model; 
      } 

      return base.BindModel(controllerContext, bindingContext); 
     } 
    } 

該粘合劑將檢查特定輸入名稱爲「動態」,然後創建動態對象



    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(dynamic input) 
    { 
     ViewBag.Result = input.phrase; 
     return View(); 
    } 

0

我想這裏的問題是確切知道你想在你的控制器之間分享實體之間共同的功能。如果所有這些功能都可以統一在一些共享屬性後面(從上面的例子中,可能是Id和Name字段?)然後,您可以來回傳遞IEntity的實例,或者包含這兩個屬性的某個基類。

3

可以接受的FormCollection,它看起來像:

public void UpdateName(FormCollection collection) 
{ 
    var person = PersonController.GetPerson(int.Parse(collection["id"])); 
    person.Name = collection["name"]; 
    person.Age = collection["age"]; 

    PersonController.UpdatePerson(person); 
} 
1

我不明白爲什麼要做到這一點,但你可以用它實現動態的(我沒有嘗試這個):

public void UpdateName(string parameters) 
{ 
    var dynamicObject = Json.Decode(parameters); 
} 

Json.Decode是System.Web.Healpers命名空間

而且你可以在JavaScript的他們擦肩而過如下:

var dataObject = JSON.stringify({ id: '1', name: 'John' }); 
$.ajax({ 
    dataType: 'json', 
    type: 'POST', 
    url: '@Url.Action("UpdateName", "Edit")', 
    data: dataObject 
}); 
相關問題