【问题标题】:"anonymous type" parameter in T4MVC ActionresultT4MVC Actionresult 中的“匿名类型”参数
【发布时间】:2014-01-17 07:03:16
【问题描述】:

我想知道我是否可以这样做,以及如何..

我有一个这样定义的动作结果:

public virtual JsonResult Created(string tableName, object where)
    {
       ....some code
    }

我正在使用 T4MVC 并尝试像这样调用操作结果:

MVC.MyController.Created("MyTable", new { Name = "Matt", Age = 11})

但是在控制器中 where 参数的类型是对象 {string[]} 它只有一个条目,看起来像这样:

where[0]="{ Name = "Matt", Age = 11 }"

有没有办法在 MyController 中将 where 参数作为匿名类型获取?

更新:

每隔几秒调用一次 Created 方法来查看数据库,如果创建了某行则返回 true。这是调用 Created 方法的方法:

public virtual ActionResult WaitingForUpdate(JsonResult pollAction, string    redirectToOnSave = null)
    {
        return View("CommandSentPartial", new CommandSentModel
                                              {
                                                  Message = "Waiting for update",
                                                  PollAction = pollAction,
                                                  RedirectTo = redirectToOnSave
                                              });
    }

然后我打电话

WaitingForUpdate(MVC.MyController.Created("MyTable", new { Name = "Matt", Age = 11}))

【问题讨论】:

  • T4MVC 不谈,你会如何在纯 MVC 中做到这一点?请参阅第 1.1 节。在t4mvc.codeplex.com/documentation
  • 感谢您的建议!我尝试这样做: new MyController().Created("MyTable", new { Name = "Matt", Age = 11}) 并且工作得很好, where 参数是控制器中的匿名类型,所以我认为是 T4MVC 问题...
  • 您尝试的是不同的,因为您正在进行直接的方法调用。使用 T4MVC 和直接 MVC,您最终会创建一个链接(一个 URL),单击该链接会调用您的操作。你能多展示一点你的代码吗?您如何使用方法调用返回的内容?例如你打电话给Html.ActionLink()?
  • 每隔几秒调用一次 Created 方法来查看数据库,如果创建了某行则返回 true。

标签: asp.net-mvc-3 t4mvc


【解决方案1】:

如果您的目标是直接调用 Create 方法,那么您不应该使用 T4MVC,而只需对其进行直接方法调用。 T4MVC 是为了帮助生成链接,这些链接稍后会通过 MVC 路由调用到 Action。你能打电话吗

this.Created(...)

直接?

【讨论】:

  • 我不这么认为。 MyController 基于通过依赖注入提供的一些输入,直接使用它意味着我必须提供这些输入...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多