【问题标题】:Create hyperlinks in MVC Action Method在 MVC 操作方法中创建超链接
【发布时间】:2010-06-24 13:16:02
【问题描述】:

我的控制器中有一个返回 JsonResult 的操作方法:

    public JsonResult GetDetails()
    {
        var rows = //Linq-To-SQL
        //Linq-To-Entities
        var lifts = (from r in rows
                     group r by new { r.LiftID, r.LiftDate } into g
                     select new
                     {
                         ID = g.Key.LiftID,
                         Date = g.Key.LiftDate.ToShortDateString(),
                         Driver = g.Where(x => x.IsDriver)
                                    .Select(x => x.p).Single().Name,
                         Passengers = g.Where(x => !x.IsDriver)
                                        .Select(x => x.p.Name)
                                        .Aggregate((x, y) => x + ", " + y)
                     }).ToList();
        return Json(lifts);
    }

我在 jQuery 脚本中使用结果来写出一个表格。

数据如下:

标识 |日期 |司机 |乘客

1 | 20/06/2010 |大卫尼尔 |约翰·史密斯、保罗·琼斯

等等……

我希望这些名称是路径Person\{id} 的超链接,即<a href="\Person\7">David Neale</a>p 属性对应于包含NameIDPerson 对象。

我不想手动构建 URL。如何使用 MVC 路由引擎构造对象以将名称包含为超链接?

【问题讨论】:

    标签: c# .net asp.net asp.net-mvc linq


    【解决方案1】:

    这很容易。

    只需使用 Url.Action("actionName", "controllerName", params)

    它将使用路由引擎创建一个字符串,因此如果您更改路由,您的代码将继续正常工作

    【讨论】:

      【解决方案2】:

      首先,我认为您的modelLinq-To-SQLLinq-To-Entities 查询有误。因为您没有ID 用于您的人员(司机和乘客),如果您想要带有人员 ID 的链接,您肯定需要它.我认为您需要将 Lifts 与您的 Persons 分开,并拥有 2 个单独的实体(当然通过其 ID 链接)。

      其次,您需要将 Person 的 ID 从 Controller 传递给 View。

      class Lift
      {
          public int LiftID { get; set; }
          public DateTime LiftDate { get; set; }
          public IEnumerable<Person> p { get; set; }
      }
      class Person
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public bool IsDriver { get; set; }
      }
      public JsonResult GetDetails()
      {
          var rows = new Lift[] { //Linq-To-SQL and Linq-To-Entities replaced by an example
              new Lift{LiftID = 1, LiftDate= DateTime.Now, p = new Person[] {
                  new Person{IsDriver = true,  Id = 1, Name = "David Neale"},
                  new Person{IsDriver = false, Id = 2, Name = "John Smith"},
                  new Person{IsDriver = false, Id = 3, Name = "Paul Jones"}
              }},
              new Lift{LiftID = 2, LiftDate= DateTime.Now, p = p = new Person[] {
                  new Person{IsDriver = true,  Id = 4, Name = "Daniel Faraday"},
                  new Person{IsDriver = false, Id = 2, Name = "John Smith"}
              }}
          };
          var lifts = (from r in rows
                       select new
                       {
                           ID = r.LiftID,
                           Date = r.LiftDate.ToShortDateString(),
                           Driver = r.p.Where(x => x.IsDriver)
                                       .Select(x => x.Name).Single(),
                           Passengers = r.p.Where(x => !x.IsDriver)
                                           .Select(x => x.Name)
                                           .Aggregate((x, y) => x + ", " + y)
                       }).ToList();
          return Json(lifts);
      }
      

      然后,一旦您在 View 上获得 ID,您就可以使用它来使用 Html.ActionLink 建立链接:

      <%= Html.ActionLink("Person", "Index", new { id = p.Id })%>
      

      【讨论】:

      • 如我所说,p 代表Person 类型的对象。这个类型确实包含一个ID 属性,我只是懒得向你展示这个类型的结构。 ActionLink HTML 帮助程序不起作用,因为该数据是由客户端的 jQuery 填充的。发回的数据必须是有效的 HTML。
      【解决方案3】:

      如果你事先知道动作和控制器,你可以这样做:

      var baseURL = '&lt;%=Url.Action("MyAction","MyController") %&gt;';

      然后从 jQuery 手动构建链接,将 href 设置为 baseUrl+"/"+personId 之类的东西

      【讨论】:

      • json 数据是通过 ajax 调用返回的,所以我不能使用服务器标签。
      • 您不能只在 (f.ex in (document).ready(...) 中的 f.ex 上全局声明它,然后简单地从您的异步响应中引用它?如果我误解了您的问题,看到一些观点可能是有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2013-08-18
      • 1970-01-01
      • 2010-10-22
      • 2018-06-19
      • 2016-01-30
      • 2012-11-07
      相关资源
      最近更新 更多