2016-08-30 114 views
0

我有一個從數據庫返回結果的小型serach框。這工作正常。結果顯示在「列表」頁面中並正確顯示。但是,我需要將選定的對象傳遞給我的控制器。當我調試它時,我得到NULL值,並且有一個空的結果頁面。 這裏是模型:MVC從列表視圖頁面傳遞所選對象

public class CodeSnip 
{ 
    public short Id { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public string Code { get; set; } 
    public LangType Language { get; set; } 
    public string Creator { get; set; } 

} 

public class ListOfCodeSnips : List<CodeSnip> 
{ 
    public CodeSnip CodeSnip { get; set; } 
} 

public enum LangType 
{ 
    CSharp,     
    css, 
    HTML, 
    JavaScript, 
    Perl, 
    PHP, 
    Python, 
    Ruby, 
    SQL, 
    VB, 
    XML, 
    Other 
} 

這裏是控制器方法(其中不執行任何大氣壓):

[HttpPost] 
    public ActionResult Display(CodeSnip snip) 
    { 
     return View(snip); 
    } 

以下是我有一個圖。再次,它的職位只NULLS爲對象的值:

@model Models.ListOfCodeSnips 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.CodeSnip.Title) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.CodeSnip.Description) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.CodeSnip.Language) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.CodeSnip.Creator) 
     </th> 
    </tr> 

    @using (Html.BeginForm("Display", "Home", FormMethod.Post)) 
    { 


    foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Title) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Language) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Creator) 
     </td> 
     <td> 
      <input type="submit" value ="Display"/> 
     </td> 
    </tr> 
     } 
    } 
</table> 

等等,總之,我想要做的就是從視圖下所選擇的模型項目,並將其傳遞到我控制器顯示方法,但我說,我所得到的都是空值。 我環顧四周,我發現的所有例子都是如何傳遞對象列表。我試圖與這些人一起瞎搞,但什麼也沒得到。

謝謝你的時間。

+0

如果你沒有做任何計算或試圖保存/改變數據庫的數據庫,而只是想顯示*他們..爲什麼使用'HttpPost'請求而不是'HttpGet'請求? –

+0

這種方法有沒有優勢,如果是的話,我該怎麼做?我嘗試了一個ActionLink,但仍然無法傳遞對象 – rigamonk

+0

rigamonk

回答

1

既然你說你已經嘗試過使用ActionLink並且它沒有工作,這裏是它將如何工作..而不是通過你正在尋找的類型作爲Display動作的參數,傳遞ID記錄。

因此,這將是這個樣子:

控制器動作

[HttpGet] 
public ActionResult Display(int id) 
{ 
    var snip = db/* connection string */.TableName.Find(id); 
    return View(snip); 
} 

ActionLink

@Html.ActionLink("Display", "Display", "ControllerName", new { id = item.Id }, null) 

// Or if you want a button that acts as a link, and not just a plain link 

<input type="button" class="btn" value="Display" onclick="location.href='@Url.Action("Display", "ControllerName", new { id = item.Id })'" /> 

讓我知道,如果這有助於!

+0

雖然我已經這樣做了,但我不想再打第二個電話到數據庫(儘管它相當便宜) – rigamonk

+0

@rigamonk好吧,但這解釋了爲什麼你點擊按鈕時得到空值的原因..你的代碼不知道你想要*顯示哪個記錄* ..你只是在調用一個空的CodeSnip模型...你需要使用'ID',因爲它對每個記錄都是唯一的 –

0

有兩個問題與您的代碼:

1)你不渲染,你所選擇的值發送回控制器的任何<input>元素。除了Html.DisplayFor之外,還使用Html.HiddenForHtml.EditorFor

2)爲了使MVC Modelbinder能夠綁定您的列表,請使用for循環代替foreach

又見MVC Form submit not transferring the updated model back to action method for List

for (var i = 0; i < Model.Count(); i++) { 
    <tr> 
     <td> 
      @Html.DisplayFor(m => m[i].Title) 
      @Html.HiddenFor(m => m[i].Title) 
     </td> 
     @* etc ... *@ 
    <tr> 
} 

PS:這個循環會後都顯示CodeSnips,即接收行動將有簽名public ActionResult Display(ListOfCodeSnips postData)