2015-07-13 39 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“和”搜索“)。你可以尝试将它插入到你的应用程序中吗?