我在我的控制器中的以下內容:爲什麼ViewBag不能正確顯示查詢結果?
public ActionResult Index(int? id)
{
int projectId = (int)id;
//Get the project name to use in the view
var projName = from p in db.Projects where p.ID == id select p.ProjectName;
ViewBag.projectName = projName.ToString();
//Rest of code ommitted
在我看來:
<p>There are no actors for the @ViewBag.projectName yet.</p>
的問題是,我的看法是呈現以下,而不是我的項目名稱:
SELECT [Extent1].[ProjectName] AS [ProjectName] FROM [dbo].[Project] AS [Extent1] WHERE [Extent1].[ID] = @p__linq__0
還沒有演員。
我試着改變代碼ViewBag.projectName = projecName.ToList();
按這個問題Passing query results in a viewbag但呈現:
There are no actors for the System.Collections.Generic.List1[System.String]
呢。
我在這裏做錯了什麼?
非常感謝。
這工作的感謝:-)你救了我的星期天。按照您的建議切換行意味着我可以使用'ViewBag.projectName = projName;'。 (以防其他人在將來遇到類似問題時閱讀此內容)。 – gazrolo4 2014-11-23 13:53:12