我正在使用上面的方法,它适用于URL中的一个参数.
例如Students/getstud/1,应用控制器/动作/参数格式的地方.
现在我在Students控制器中有一个动作,它接受两个参数并返回一个JSON对象.
那么如何$.getJSON()使用post方法发布数据?
类似的方法也是可以接受的
关键是用AJAX调用控制器的动作.
我当前的代码如下所示.如何将我的数组传递给控制器以及控制器操作必须接受哪种参数?
function getplaceholders() {
var placeholders = $('.ui-sortable');
var result = new Array();
placeholders.each(function() {
var ph = $(this).attr('id');
var sections = $(this).find('.sort');
var section;
sections.each(function(i, item) {
var sid = $(item).attr('id');
result.push({ 'SectionId': sid, 'Placeholder': ph, 'Position': i });
});
});
alert(result.toString());
$.post(
'/portal/Designer.mvc/SaveOrUpdate',
result,
function(data) {
alert(data.Result);
}, "json");
};
Run Code Online (Sandbox Code Playgroud)
我的控制器动作方法看起来像
public JsonResult SaveOrUpdate(IList<PageDesignWidget> widgets)
Run Code Online (Sandbox Code Playgroud) JsonResult类是一种非常有用的方法,可以通过AJAX将Json作为动作返回给客户端.
public JsonResult JoinMailingList(string txtEmail)
{
// ...
return new JsonResult()
{
Data = new { foo = "123", success = true }
};
}
Run Code Online (Sandbox Code Playgroud)
然而(至少根据我的第一印象)这真的不是一个很好的分离关注点.
我想知道为什么对象和Json之间的转换没有通过属性以声明方式实现.在下面的代码中,您基本上告诉MVC this method is convertible to Json,然后如果从AJAX客户端调用它,则检查new JsonResult()内部执行转换的属性.
单元测试可以只采取动作结果(ObjectActionResult)并拉出强类型Foo.
[JsonConvertible]
public ActionResult JoinMailingList(string txtEmail)
{
// ...
return new ObjectActionResult()
{
Data = new Foo(123, true)
};
}
Run Code Online (Sandbox Code Playgroud)
我只是好奇人们的想法和任何可供选择的模式.
这些也只是我最初的观察 - 可能还有更多理由说明为什么这不是一个理想的设计(可能还有很多理由为什么它是一个完全可以接受和实用的设计!)我今晚只是感觉理论和魔鬼 - 拥护者.
*免责声明:我甚至没有开始考虑如何实施属性或者它可能具有哪些副作用或重复性等.
我有一个Web API控制器,从那里我将一个对象作为JSON从一个动作返回.
我是这样做的:
public ActionResult GetAllNotificationSettings()
{
var result = new List<ListItems>();
// Filling the list with data here...
// Then I return the list
return new JsonResult { Data = result };
}
Run Code Online (Sandbox Code Playgroud)
但是这样,包含其Data属性的JsonResult对象被序列化为JSON.所以我通过动作返回的最终JSON如下所示:
{
"ContentEncoding": null,
"ContentType": null,
"Data": {
"ListItems": [
{
"ListId": 2,
"Name": "John Doe"
},
{
"ListId": 3,
"Name": "Jane Doe"
},
]
},
"JsonRequestBehavior": 1,
"MaxJsonLength": null,
"RecursionLimit": null
}
Run Code Online (Sandbox Code Playgroud)
我无法序列化此JSON字符串,因为该JsonResult对象向其添加了各种其他属性.我只对其感兴趣ListItems,别的什么.但它自动添加的东西,如:ContentType,MaxJsonLength等...
现在这对我来说不起作用,因为JSON字符串中的所有其他属性......
var …Run Code Online (Sandbox Code Playgroud) 如何使用Ajax.ActionLink从控制器方法实际获取JSON?我尝试搜索网站,但最接近的是ASP.NET MVC控制器操作返回JSON或部分html
并且"最佳答案"实际上并没有告诉您如何从ajax.actionlink中的SomeActionMethod获取JSON.
我正试图让我的apicontroller工作.但不知怎的,我不能回来Json().
这是编译器的错误消息:
错误CS0029无法将类型'System.Web.Http.Results.JsonResult <>'隐式转换为'System.Web.Mvc.JsonResult'Opten.Polyglott.Web D:\ Development\git\Opten.Polyglott\src\Opten.Polyglott名.web \控制器\ NewsletterApiController.cs
我无法解释为什么它无法转换Json()为ActionResult偶数的Json()继承ActionResult.
这是我的控制器:
using MailChimp;
using MailChimp.Helper;
using Opten.Polyglott.Web.Models;
using Opten.Umbraco.Common.Extensions;
using System.Configuration;
using System.Web.Mvc;
using Umbraco.Core.Logging;
using Umbraco.Web.WebApi;
namespace Opten.Polyglott.Web.Controllers
{
public class NewsletterApiController : UmbracoApiController
{
public ActionResult Subscribe(Newsletter newsletter)
{
bool isSuccessful = false;
if (ModelState.IsValid)
{
isSuccessful = SubscribeEmail(newsletter.Email);
}
return Json(new { isSuccess = isSuccessful });
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
asp.net-mvc ×4
c# ×3
json ×3
jquery ×2
asp.net-ajax ×1
controller ×1
getjson ×1
http-post ×1
javascript ×1
json.net ×1
post ×1