2012-08-30 80 views
0
return (from doc in db.setupDocuments 
       where doc.ParentDocumentID == parentId 
       select new TreeViewItem 
       { 
        Text = doc.DocumentTitle, 
        Value = SqlFunctions.StringConvert((decimal)doc.DocumentID), 
        LoadOnDemand = doc.setupDocuments1.Count > 0, 
        Enabled = true, 
        RouteName = 
        //Url = "/Settings/SelectedItem?text=" + doc.DocumentTitle 
       }); 

嗨,朋友,我想创建一个链接,每个对象从数据库中检索。我想将它转发到对象标题名称的Action(例如doc.DocumentTitle)和静态控制器“Settings”。但不要使用评论栏中给出的链接。当我使用 ActionName = doc.DocumentTitle,
ControllerName =“设置” 它没有工作......任何建议。提前致谢。使用Linq查询创建超链接

回答

0

您应该使用UrlHelper在ASP.NET MVC应用程序中生成链接。例如控制器具有Url属性,你可以使用:

public ActionResult SomeAction() 
{ 
    ... 

    from doc in db.setupDocuments 
    where doc.ParentDocumentID == parentId 
    select new TreeViewItem 
    { 
     Text = doc.DocumentTitle, 
     Value = SqlFunctions.StringConvert((decimal)doc.DocumentID), 
     LoadOnDemand = doc.setupDocuments1.Count > 0, 
     Enabled = true, 
     RouteName = Url.Action("SelectedItem", "Settings", new { text = doc.DocumentTitle }) 
    }); 

    ... 
} 

如果你需要在其它的类来使用这个,你可以从控制器到它通过UrlHelper的一个实例。

+0

非常感谢您的回复,但问题在于查询不是写入某个控制器操作,而是存储在存储库中,因此无法访问Url.Action(..,..,..)。 有没有其他解决方案? –

+1

在这种情况下,您可以将UrlHelper实例作为参数传递给需要使用它的方法。 –