代码之家  ›  专栏  ›  技术社区  ›  David Neale

在MVC操作方法中创建超链接

  •  0
  • David Neale  · 技术社区  · 14 年前

    我有一个动作方法返回 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 属性对应于 Person 包含两者的对象 Name ID .

    I don't want to construct the URL manually. 我将如何使用MVC路由引擎构造包含超链接名称的对象?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Matteo Mosca    14 年前

    这相当容易。

    只需使用url.action(“actionname”,“controllername”,params)

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

        2
  •  0
  •   Mariano Desanze    14 年前

    首先,我认为你的 model 或者在你的 Linq-To-SQL Linq-To-Entities 奎斯。因为你没有 ID 为你的人 (司机和乘客),如果你想的话,你肯定需要它。 带有此人ID的链接 . 我想你需要把你的 升降机 从你 珀森斯 并拥有 2个独立实体 (当然是通过它的ID链接的)。

    其次,您需要将人员的ID从控制器传递到视图。

    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);
    }
    

    然后,一旦在视图上有了ID,就可以使用它来创建链接 Html.ActionLink :

    <%= Html.ActionLink("Person", "Index", new { id = p.Id })%>
    
        3
  •  0
  •   Terje    14 年前

    如果您事先知道操作和控制器,您可以执行以下操作:

    var baseURL = '<%=Url.Action("MyAction","MyController") %>';

    然后手动从jquery构建链接,并将href设置为类似 baseUrl+"/"+personId