2015-07-13 44 views
1

我有一個控制器3個actionResults,我希望所有actionResults返回一個視圖作爲代碼如下:試圖調用視圖錯誤的操作方法的結果

在控制器:

public ActionResult Index() 
     { 
      return View(ListGroup); 
     } 

[HttpPost] 
public ActionResult Index(List<Group> listModel) { 
    @ViewBag.Success = "Update Suceess"; 
    return View(listModel);//I set break point here 
} 

[HttpPost] 
public ActionResult Search(Group ModelSearch) { 
    List<Group> listResult = ListGroup.Where(m=>m.GroupID == ModelSearch.GroupID).ToList(); 
    return View("Index", listResult); 
} 

鑑於我有兩種形式:

@using (Html.BeginForm("Search", "DisplayTable")) 
    { 
     <fieldset> 
      <legend>Search</legend> 
      <input type="text" name="GroupID" /> 
      <input type="submit" value="SEARCH" /> 
     </fieldset> 
    } 

    @using (Html.BeginForm("Index", "DisplayTable", FormMethod.Post)) 
    { 
     var i = 0; 
    <table> 
     <tr> 
      <td>Name</td> 
      <td>GroupID</td> 
     </tr> 
     @foreach(var item in Model){ 
      <tr> 
       <td>@Html.TextBoxFor(m => m[i].Name)</td> 
       <td>@Html.TextBoxFor(m => m[i].GroupID)</td> 
      </tr> 
      i++; 
     } 
    </table> 
     <input type="submit" value="SAVE" /> 
    } 

有兩兩件事我想這個控制器做:

  1. 根據輸入搜索記錄庫。

  2. 編輯記錄。

我把每個函數都放到actionResult中。 ActionResult指數工作正常,但actionResult搜索不起作用,它沒有事件轉到我設置的斷點。

+3

嘗試在視圖中添加'FormMethod.Post'並在控制器中製作操作'public' – Tushar

+0

控制器的名稱是什麼? –

+0

@HemantBhagat由於他說'Index'方法正在工作,它應該是'DisplayTableController'。 – InvisiblePanda

回答

0

您試圖接收組的對象在你的搜索方法,如果是這樣,你認爲應堅決與集團模式

其他明智的,正確的搜索行動方法

[HttpPost] 
public ActionResult Search(int? GroupId) { 
    List<Group> listResult = ListGroup.Where(m=>m.GroupID == GroupId).ToList(); 
    return View("Index", listResult); 
} 
+0

我不認爲這是正確的。他的控制器是'DisplayTableController',而不是'Index'。 – InvisiblePanda

+0

是的你是對的。感謝您的糾正。 – blue

0

我打字不知道實際問題是什麼。由於含糊不清,它可能與ModelBinder無法匹配某些屬性名稱,但我們不知道所有涉及的類。

我試圖重現問題如下,但不能這樣做,因此我可以給你一個工作(至少對我來說)你要做的事情的例子。我將列出我在這裏創建的所有內容,以便您可以使用它並查看是否仍然出現錯誤。

我也冒昧地重構你的觀點。你並不真正需要的是醜陋var i=0i++ ;-)

Group類:

public class Group 
{ 
    public int GroupID { get; set; } 
    public string Name { get; set; } 
} 

Index觀點:

@model IList<WebApplication1.Models.Group> 

@using (Html.BeginForm("Search", "DisplayTable")) 
{ 
    <fieldset> 
    <input type="text" name="GroupID" /> 
    <input type="submit" value="SEARCH" /> 
    </fieldset> 
} 

@using (Html.BeginForm("Index", "DisplayTable")) 
{ 
    <table> 
    <tr> 
     <td>Name</td> 
     <td>GroupID</td> 
    </tr> 

    @for (int i = 0; i < Model.Count; i++) 
    { 
     <tr> 
     <td>@Html.TextBoxFor(model => model[i].GroupID)</td> 
     <td>@Html.TextBoxFor(model => model[i].Name)</td> 
     </tr> 
    } 
    </table> 
    <input type="submit" value="SAVE" /> 
} 

控制器:

public class DisplayTableController : Controller 
    { 
    private List<Group> groups = new List<Group> 
     { 
      new Group { GroupID = 1, Name = "Group 1" }, 
      new Group { GroupID = 2, Name = "Group 2" } 
     }; 

    public ActionResult Index() 
    { 
     return View(groups); 
    } 

    [HttpPost] 
    public ActionResult Index(List<Group> viewModel) 
    { 
     return View(viewModel); 
    } 

    [HttpPost] 
    public ActionResult Search(Group group) 
    { 
     var result = groups.Where(g => g.GroupID == group.GroupID).ToList(); 
     return View("Index", result); 
    } 
    } 

這絕對適合我(「SA VE「和」搜索「)。你可以嘗試將它插入到你的應用程序中嗎?