2012-03-19 62 views
2

我正在創建一個名爲AskQuestion的應用程序,其中在第一頁我有一個問題列表作爲鏈接,下面是一個textarea和一個post按鈕張貼問題。 當我寫在textarea裏面並按下發布按鈕時,它顯示在問題列表中。 現在我從數據庫中提取發佈的問題。我需要顯示其作爲鏈接.. 但Html.Actionlink不支持此顯示在Html.Actionlink()中從數據庫中獲取的值mvc3

這是我的模型類:

public class Question_Page 
{ 

    public virtual int Id { get; set; } 
    public virtual string Question_name { get; set; } 
    public virtual string Address { get; set; } 
    public virtual DateTime Created_Date { get; set; } 
    public virtual DateTime Modified_Date { get; set; } 
    public virtual int Created_By { get; set; } 
    public virtual int Modified_By { get; set; } 
    public virtual char Deleted { get; set; } 
    public Question_Page() 
    { 
     this.Deleted='F'; 
    } 
} 

這是我的看法:

@model IEnumerable<Core.Model.Question_Page> 

@{ 
    ViewBag.Title = "Index"; 
} 
<style type="text/css"> 
ul 
{ 
    list-style-type: none; 
} 
</style> 

<h2>All Questions</h2> 
<hr /> 

@using (Html.BeginForm("Index","Question",FormMethod.Post)) 
{ 
@Html.ValidationSummary(true) 
<ul> 
foreach (var item in Model) { 

    <li>@Html.ActionLink(item.Question_name, "answer", new { id=item.Id })</li> 
} 
    </ul> 
<label for="PostyourQuestion:">Post your Question:</label><br /><br /> 
@Html.TextArea("textID")  

<br /> 
<input type="submit"/> 
} 

但與此我得到一個錯誤,說「項目不存在於當前的情況下」

我知道我可以使用錨標記之前,我也試過這個

<ul> 
foreach (var item in Model) { 

    <li><a href="answer.cshtml">@Html.DisplayFor(modelItem => item.Question_name)</a></li> 
} 
</ul> 

但它並沒有確定的href

指定的網址,請幫我

+0

爲什麼問題鏈接列表是一個FORM標記內? – Romias 2012-03-19 13:02:11

+0

你在'foreach'之前忘了'@'嗎? – tobias86 2012-03-19 13:10:54

回答

1

我做了一個樣本,它看起來像你的,我發現你錯過了「@」的foreach之前「 「

@foreach (var item in Model) { 
+0

我已更新我的問題PLZ看看它 – user1274646 2012-03-19 13:01:17

+0

我更新了我的答案 – Rinat 2012-03-19 13:23:05

+0

是的行爲一個非常愚蠢的錯誤BTN NSX thanx 4指出 – user1274646 2012-03-20 02:17:25

1

第一個帶有」Html.ActionLink「的應該可以工作。問題可以在你的模型中。如果你的模型返回null,那麼這個模型中的item將導致foreach循環內的錯誤,你應該首先檢查Model null狀態。 我認爲Html.ActionLink方法在你的情況下非常有用。但是你應該通過論證來確定你的問題,即它的ID。然後你可以使用這樣的事情:

    的foreach(VAR項模型){

    <li>@Html.ActionLink(item.Question_name, "answer",new{id= item.id})</li> 
    

    }

+0

我已更新我的問題PLZ看看它 – user1274646 2012-03-19 13:00:56

+0

是的,@是在foreach之前缺少 – cemsazara 2012-03-19 13:30:35

+0

耶是一個非常愚蠢的錯誤,因爲它現在工作thanx :) – user1274646 2012-03-20 02:17:45

相關問題