0

i am new to mvc. i want to send list of data from view to controller. i can pass one employee details but can't pass list . please give some suggestion for sort it out.

Error:-enter image description here

i need to get the Lidt in controller.

  public class EmpDetails
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int OldSeq { get; set; }
    public int NewSeq { get; set; }
}
public class DetailsList
{
    public int Id { get; set; }
    public IEnumerable<EmpDetails> EmpDetailsList { get; set; }
}

In controller have two method get and post. In post method getting id but not list.it is coming as null.

[HttpGet]
    public ActionResult EmployeeDetails()
    {
        List<EmpDetails> c2 = new List<EmpDetails>();
        EmpDetails model = new EmpDetails();


        model = new EmpDetails();
        model.Id = 2;
        model.Name = "Alex";
        model.OldSeq = 4;
        model.NewSeq = 5;
        c2.Add(model);

        DetailsList abc = new DetailsList();
        abc.Id = 1;
        abc.EmpDetailsList = c2;
        return View(abc);
    }
    [HttpPost]
    public ActionResult EmployeeDetails(DetailsList model)
    {

        return View("EmployeeDetails");
    }

In view i'm looping the data and displaying.

      @using MvcDemo.Models
      @model  DetailsList

       @using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
     @Html.EditorFor(modelItem => modelItem.Id)
    <table>
        @foreach (var item in Model.EmpDetailsList)
        {
            <tr>
                <td>
                    @Html.EditorFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => item.OldSeq)
                </td>
                <td>
                     @Html.TextBoxFor(modelItem => item.NewSeq)
                </td>

            </tr>
        }
    </table>
        <input type="submit" value="Create" />
}
nichu09
  • 882
  • 2
  • 15
  • 44
  • 1
    You cannot use a `foreach` loop. You must use a `for` loop (the property must be `IList` or you must use a custom `EditorTemplate` Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for more detail –  Oct 13 '15 at 09:24
  • thanks for the reply.Could you please explain why i need to use for loop ? – nichu09 Oct 13 '15 at 09:26
  • @StephenMuecke thanks i will look into it – nichu09 Oct 13 '15 at 09:29
  • 1
    Refer also [this answer](http://stackoverflow.com/questions/31954735/model-binding-with-complex-type/31954778#31954778) for an example of the `EditorFor()` approach –  Oct 13 '15 at 09:36
  • 1
    @nichu09 - this is the way the binding will work by using the `for` loop approach, the generated html will be a match for your model and your `post` will work. – Ric Oct 13 '15 at 09:44
  • @StephenMuecke Based on your comment i changed to List and to for loop.it worked. thanks . i will update the correct answer – nichu09 Oct 13 '15 at 09:46
  • @Ric when using for loop , it creating unique id for each control. if i am using for each loop it is not happening, is that the actual problem ? or i missing anything ? – nichu09 Oct 13 '15 at 09:47
  • @nichu09 - try the `foreach` and `for` approach and inspect the html on the page and you will notice the difference. – Ric Oct 13 '15 at 09:56
  • @Ric i checked the html . In for each loop control id are same in every row. but in for loop id are different. Thanks – nichu09 Oct 13 '15 at 10:01
  • 1
    exactly! when you visualize it, it makes more sense. – Ric Oct 13 '15 at 10:02

3 Answers3

1

Use editor templates like this.

  1. Create EditorTemplates folder in your Views/Shared folder.
  2. Create a file named EmpDetails.cshtml in that folder.
  3. Put your foreach markup in that file like

    @model EmpDetails
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model.Id)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model.Name)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model.OldSeq)
        </td>
        <td>
            @Html.TextBoxFor(modelItem => Model.NewSeq)
        </td>
    </tr>
    
  4. Then replace your foreach with a call to @Html.EditorTemplateFor method like

    @using MvcDemo.Models
    @model  DetailsList
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        @Html.EditorFor(modelItem => modelItem.Id)
        <table>
            @Html.EditorFor(m=>m.EmpDetailsList)
        </table>
        <input type="submit" value="Create" />
    }
    
Dandy
  • 2,177
  • 14
  • 16
0
 public ActionResult EmployeeDetails()
    {
        List<EmpDetails> c2 = new List<EmpDetails>();
        EmpDetails model = new EmpDetails();


        model = new EmpDetails();
        model.Id = 2;
        model.Name = "Alex";
        model.OldSeq = 4;
        model.NewSeq = 5;
        c2.Add(model);

        DetailsList abc = new DetailsList();
        abc.Id = 1;
 //  abc.EmpDetailsList = c2;

//change this might help
    abc.EmpDetailsList = new List<EmpDetails>(c2);


        return View(abc);
    }
DropAndTrap
  • 1,538
  • 16
  • 24
0

Thanks for the quick reply.With the help of @stephen comment , i changed the IEnumerable collection into List. then i used for loop instead of for each. please check the below code.

 public class DetailsList
{
    public int Id { get; set; }

    public List<EmpDetails> EmpDetailsList { get; set; }
}

 <table>
     @for(int item = 0; item < Model.EmpDetailsList.Count;item++ )
     {
            <tr>
                <td>
                    @Html.EditorFor(modelItem => modelItem.EmpDetailsList[item].Id)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => modelItem.EmpDetailsList[item].Name)
                </td>
                <td>
                    @Html.TextBoxFor(modelItem => modelItem.EmpDetailsList[item].OldSeq)
                </td>
                <td>
                     @Html.TextBoxFor(modelItem => modelItem.EmpDetailsList[item].NewSeq)
                </td>

            </tr>
     }
        </table>
nichu09
  • 882
  • 2
  • 15
  • 44