【问题标题】:Sending json to asp.net将 json 发送到 asp.net
【发布时间】:2014-04-27 09:52:45
【问题描述】:

我有一个带有GetSave 方法的控制器。 GetData 方法将向前端发送 JSON。我在这个博客之后做了这个。

http://johnnycode.com/2012/07/16/getting-json-from-asp-net-beautiful-good-bad-ugly/

现在我不确定如何实现 Save()。
由于我返回的是 JSON,因此我没有视图模型。
UX 方面由不同的团队处理。
因此,如果有人能告诉我如何将 JSON 发送回 Save(),将会很有帮助。

目前我的存档是这样的

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

[HttpGet]
public ActionResult GetData()
{
    return new JsonNetResult() { Data = this.aggregatedUserAppSettings.GetAllUserAppSettings() };
}

[HttpPost]
public ActionResult Save(JObject setting)
{
}

我希望设置变量的值应为{{property1: value1},{property2: value2}}

前端应该如何传递 JSON 来实现这一点?

【问题讨论】:

    标签: asp.net-mvc json json.net


    【解决方案1】:

    将您的操作方法的返回类型更改为JsonResult,然后您可以使用匿名对象返回您需要的任何 Json:

    [HttpPost]
    public JsonResult Save(JObject setting)
    {
        //logic to save here
    
        //use anonymous object to return whatever json you want here
        return Json( new {success = true, data = yourData} )
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多