如何将IEnumerable对象或数据从视图传递给控制器​​?

时间:2012-08-22 12:52:36

标签: asp.net-mvc-3 razor radiobuttonfor

我创建了一个强类型视图。我想将IEnumerable Object或数据从View传递给Controller。以下是我的模型控制器查看

我的型号:

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

我的控制器

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

我的查看

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

我在中没有任何价值。当我在其上设置断点时,它在中显示 NULL 我完全对如何将值传递给控制器感到困惑。

谢谢你, Amol

2 个答案:

答案 0 :(得分:2)

而不是@Html.DisplayFor使用@Html.EditorFor,这样就会在您的表单中进行数据绑定。

此外,由于您正在使用集合,因此需要迭代字段(简化示例):

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

                    }

答案 1 :(得分:0)

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