2012-08-30 82 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實例作爲參數傳遞給需要使用它的方法。 –