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 搜索不起作用,它没有到达我设置的断点。

4

2 回答 2

0

如果是这样,您正在尝试在搜索方法中接收 Group 的对象,您的视图应该使用 Group modal 强类型

其他明智的更正您的搜索操作方法

[HttpPost]
 public ActionResult Search(int? GroupId) { 
     List<Group> listResult = ListGroup.Where(m=>m.GroupID == GroupId).ToList();
     return View("Index", listResult);
 }
于 2015-07-13T06:26:11.530 回答
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);
    }
  }

这绝对适合我(“保存”和“搜索”)。您可以尝试将其插入您的应用程序吗?

于 2015-07-13T08:07:43.800 回答