How to pass IEnumerable object or data from view to controller?

10,368

Solution 1

 @for (int i = 0; i < Model.length; i ++)
 {
 <tr>
    <td>
        @Html.DisplayFor(model => Model[i].EmailID)
    </td>
     <td>
        @Html.DisplayFor(model => Model[i].Password)
     </td>
     <td>
          @Html.DisplayFor(model => Model[i].FirstName)
     </td>
     <td>
        @Html.DisplayFor(model => Model[i].LastName)
     </td>
     </tr> 
   }
 </table>
      <input  type="submit" value="submit" />     
 }

Solution 2

Instead of @Html.DisplayFor use @Html.EditorFor, that way databinding will happen in your form.

Also since you are using collection you need to iterate your fields (simplified example):

                    @for (int row = 0; row < Model.Count; row++)
                    {
                            @Html.EditorFor(x => x[row].FirstName )

                    }
Share:
10,368
Amol
Author by

Amol

Updated on June 04, 2022

Comments

  • Amol
    Amol almost 2 years

    I have created a strongly type view. I want to pass IEnumerable Object or data from View to Controller. Following are my Model, Controller ,view

    My Model:

    public class UserDetailsClass
    {
        public static FeedbackDatabaseDataContext context = new FeedbackDatabaseDataContext(); 
        public class Tablefields
        {
            [Key]
            public  int ID { get; set; }
    
            [Required(ErrorMessage = "Email is required")]            
            public  string EmailID { get; set; }
    
            [Required(ErrorMessage="Password is required")]
            public string Password { get; set; }
    
            [Required(ErrorMessage = "First Name is required")] 
            public  string FirstName { get; set; }
    
            [Required(ErrorMessage = "Last Name is required")] 
            public string  LastName { get; set; }
        }
        public static List<UserDetails> getalluser()
        {
            var lst = (from r in context.UserDetails select r);
            return lst.ToList();        
        }
     }
    

    My Controller

     public ActionResult Test()
        {
    
            IList<UserDetailsClass.Tablefields> viewmodel = new List<UserDetailsClass.Tablefields>();
            var q = UserDetailsClass.getalluser().ToList();
    
            foreach (SQLOperation.Models.UserDetails item in q)
            {
                UserDetailsClass.Tablefields viewItem = new UserDetailsClass.Tablefields();
    
                viewItem.EmailID = item.Email;
                viewItem.FirstName = item.FirstName;
                viewItem.LastName = item.LastName;
                viewItem.Password = item.Password;
    
                viewmodel.Add(viewItem);
            }
            return View(viewmodel);
        }
    
        [HttpPost]
        public ActionResult Test(IEnumerable<UserDetailsClass.Tablefields> items)
        {
            return View();
        }
    

    My View:

    @model  IEnumerable<SQLOperation.Models.UserDetailsClass.Tablefields>
    
    @{
        ViewBag.Title = "Test";
     }
    
     <h2>Test</h2>
    
    @using (Html.BeginForm())
    {
    <table>
    <tr>
        <th>
            EmailID
        </th>
        <th>
            Password
        </th>
        <th>
            FirstName
        </th>
        <th>
            LastName
        </th>
        <th></th>
    </tr>
    
    @foreach (var item in Model)
    {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmailID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
       </tr> 
       }
    </table>
      <input  type="submit" value="submit" />     
    }
    

    I didn't get any value in items.When I set breakpoint on it it show NULL in items I am totally Confused about how to pass values to controller.

    Thank you, Amol

  • Amol
    Amol over 11 years
    Thank You, I have changed Html.DisplayFor to Html.EditorFor but I can't get values in controller. It gives NULL
  • Eduard
    Eduard over 11 years
    Edited to reflect that you are sending collection.
  • Amol
    Amol over 11 years
    i get error in above x=>x[row].FirstName thats why I change this like following. but still I doesn't get any value in controller @for(int row = 0; row < Model.Count(); row++) { @Html.EditorFor(x => x.ElementAt(row).FirstName) }
  • Amol
    Amol over 11 years
    I get values in Model but i didn't return to controller.
  • Amol
    Amol over 11 years
    Thank you, I get values in my view but i can't pass it to my controller. in my controller i get NULL