【发布时间】:2013-06-16 00:25:23
【问题描述】:
当我在 mvc 4 中的服务器端操作方法上将参数接受为动态时,我收到了这个长错误。
{"Message":"有一个错误 发生。","ExceptionMessage":"'Newtonsoft.Json.Linq.JObject' 没有 包含一个定义 'TournamentId'","ExceptionType":"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException","StackTrace":" 在 CallSite.Target(Closure , CallSite , Object )\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,Tret](CallSite 站点,T0 arg0)\r\n 在 ManagerDeTorneos.Web.Controllers.TournamentDateController.Create(对象 数据)在 F:\Prince\Projects\Juan\trunk\ManagerDeTorneos.Web\Controllers\TournamentDateController.cs:line 133\r\n at lambda_method(闭包,对象,对象[])\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c_DisplayClass13.b_c(Object 实例,Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(对象 实例,Object[] 参数)\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancelToken)"}
[HttpPost]
public HttpResponseMessage AddMatch(dynamic data)
{
int tournamentDateId = (int)data.TournamentDateId.Value;
var tournamentDate = Catalog.TournamentDateRepository.GetById(tournamentDateId);
if (tournamentDate == null)
{
throw ExceptionHelper.NotFound("Fecha no encontrada!");
}
在上述方法中的 data 包含从 ajax 调用发送的锦标赛 ID 为 JSON.Stringify({'TournamentId':'5'}).
谁能告诉我错误的原因是什么。我什至也替换了 Newtonsoft.Json 的 dll
【问题讨论】:
-
我不确定,但不是
dynamic,而是传递object作为操作方法参数是个好主意..
标签: javascript asp.net-mvc json asp.net-web-api json.net