1

I have 3 actionResults in one controller, and I want all actionResults return one view as the code below:

In controller:

 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);
 }

In view i have two forms:

 @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" />
    }

There are two things I want this controller to do:

  1. Search record base on the input.

  2. Edit record.

I put each of function to actionResult. The ActionResult Index works well, but the actionResult Search doesn't work, it didn't event go to breakpoint that I set.

InvisiblePanda
  • 1,589
  • 2
  • 16
  • 39
Tran Duy Linh
  • 221
  • 1
  • 9

2 Answers2

0

You are trying to receive the object of Group in your search method if it is so, your view should be strongly typed with Group modal

Other wise correct your search action method

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

I don't know what the actual problem is. It could be something with the ModelBinder not being able to match some property name due to ambiguity, but we don't know all the involved classes.

I have tried to reproduce the problem as follows, but couldn't do so, hence I can give you a working (for me, at least) example of what you're trying to do. I'll list everything I created here so you can use it and see if you still get an error.

I also took the liberty to refactor your view. You don't really need that ugly var i=0 and i++ ;-)

Group class:

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

The Index view:

@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" />
}

The controller:

  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);
    }
  }

This definitely works perfectly for me ("SAVE" and "SEARCH"). Could you try plugging this into your application?

InvisiblePanda
  • 1,589
  • 2
  • 16
  • 39