【问题标题】:Passing and receiving routedictionary values in mvc2在 mvc2 中传递和接收路由字典值
【发布时间】:2010-08-18 17:28:02
【问题描述】:

我的操作中有这样的链接

<%: Html.ActionLink("View Data","Index","MyItem", new {itemId=Model.itemId}, null)%>

现在在我的 newItem 控制器的索引操作中,我如何检索这个 itemId,到目前为止我已经尝试过 这个

public ActionResult Index()
        {
            RouteData rd = this.RouteData;
            var list = from p in rd.DataTokens.Where(p => p.Key == "itemId") select p;
            int? id = null;
            if (list.Count() > 0)
                id = Convert.ToInt32(list.Single().Value);
            if (id.HasValue)
            {

                return View(from a in _Service.List().Where(a => a.ApplicantId == id.Value) select a);
            }
            else
                return View(NORECORDVIEW,);


        }

还有这个

HttpRequestBase rd = this.Request;
            var list = from p in rd.QueryString.AllKeys.Where(p => p. == "itemId") select p;
            int? id = null;
            if (list.Count() > 0)
                id = Convert.ToInt32(list.Single().Value);
            if (id.HasValue)
            {

                return View(from a in Service.List().Where(a => a.ApplicantId == id.Value) select a);
            }
            else
                return View(NORECORDVIEW);

但是这些都没有返回正确的值。

在 mvc1 中,我们可以轻松地直接处理,但请在 mvc2 中做到这一点。

【问题讨论】:

  • 我不太清楚你想要完成什么......你为什么不能这样做:ActionResult Index(int? id)

标签: asp.net-mvc asp.net-mvc-2 dictionary routes


【解决方案1】:

您可以更轻松地做到这一点

public ActionResult Index(int itemId)

这不仅是代码的 1/10,而且更容易测试。

【讨论】:

  • 当然我知道,但是我正在寻找其他的东西。
  • 我想在没有索引参数的情况下完成这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 2016-01-17
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多