如何将文本框值从强类型视图(@model列表<viewmodel>)传递到控制器



查看

@model List<PMS.View_Model._ContributeViewModel>
@{
    ViewBag.Title = "Contribute";
}
@using (Html.BeginForm())
{
    @Html.Partial("_Project")
    @Html.ValidationSummary(true)
      <table>
        <tr>
              <th>
                  <u>Current Members</u>
              </th>
          </tr>
      <tr>
          <th>
              ProjectID
          </th>
          <th>
              Name
          </th>
          <th>
              Role
          </th>
      </tr>
        @foreach(var item in Model.Where(m=>m.Status==1))
            {
          <tr>
              <td>             
                 @Html.DisplayFor(model=>item.ProjectID)
              </td>
              <td>
                 @Html.DisplayFor(model=>item.Name)
              </td>
              <td>
                 @Html.DisplayFor(model=>item.RoleName)
              </td>
          </tr>
        } 
          <tr>
              <th>
                  <u>Invited Members</u>
              </th>
          </tr>
          <tr>
              <th>
                  ProjectID
              </th>
              <th>
                  Name
              </th>
          </tr>
      @foreach(var item2 in Model.Where(m=>m.Status==0))
            {       
          <tr>             
              <td>
                  @Html.DisplayFor(model=>item2.ProjectID)
              </td>
              <td>
                 @Html.DisplayFor(model=>item2.Name)
              </td>

              </tr>
            } 
          <tr>
              <td>
                  @Html.TextBoxFor(?)
              </td>
              <td>
                  <input type="submit" id="btnsearch" value="Search"/>
              </td>
          </tr>
    </table>
}

视图模型

public class _ContributeViewModel
    {
        public int UserID { get; set; }
        [StringLength(20)]
        public string Name { get; set; }
        [StringLength(20)]
        public string Email { get; set; }
        [StringLength(20)]
        public string Facebook { get; set; }
        [StringLength(20)]
        public string Twitter { get; set; }
        [StringLength(20)]
        public string LinkedIn { get; set; }
        [StringLength(20)]
        public string Github { get; set; }
        [StringLength(50)]
        public string Password { get; set; }
        [StringLength(50)]
        public string Photo { get; set; }
        [StringLength(100)]
        public string Address { get; set; }
        [StringLength(50)]
        public string PhoneNo { get; set; }
        [StringLength(500)]
        public string Other { get; set; }
        public DateTime CreateDate { get; set; }
        public int RoleID { get; set; }
        public int ProjectID { get; set; }
        [StringLength(20)]
        public string RoleName { get; set; }
        public int Status { get; set; }
        }

控制器

[HttpGet]
        public ActionResult Contribute(int? projectID)
        {

                PMSDBContext PMP = new PMSDBContext();
            //List<_ContributeViewModel> cvm = new List<_ContributeViewModel>();
            //    _Contribute cv=new _Contribute();
            //    cvm = cv.GetMember();
                List<_ContributeViewModel> result = new List<_ContributeViewModel>();
                result = PMP.Relations
                       .Join(PMP.Roles, a => a.RoleID,
                       b => b.RoleID,
                       (a, b) => new { Relation = a, Role = b })
                       .Join(PMP.Users,
                       a => a.Relation.UserID,
                       c => c.UserID,
                       (a, c) => new { a.Relation, a.Role, Users = c })
                       .Where(a => a.Relation.ProjectID == 1)
                       .Select(a => new _ContributeViewModel
                       {
                           ProjectID = a.Relation.ProjectID,
                           RoleName = a.Role.RoleName,
                           Name = a.Users.Name,
                           Status = a.Relation.Status,
                       }
                       ).ToList();
                 return View(result);
        }
        [HttpPost]
        public ActionResult Contribute(_ContributeViewModel Model)
        {
            return View();
        }

我面临的问题是。。我无法使用@Html.TextBoxFor(m=>Model.Name或Something)将文本框值传递给控制器。。是因为列表?但是如果我删除List<>。。我在foreach循环中遇到问题。。。我应该对此文本框使用局部视图吗?或者有比使用局部视图更好的方法吗?请建议。。。

您可以通过以下方式使用TextBox()

 @{ 
   int index = 0; 
  } 
@foreach(var item2 in Model.Where(m=>m.Status==0))
{
  @Html.TextBox("Model["+index.ToString()+"].ProjectID",item2.ProjectID)
  @Html.TextBox("Model["+index.ToString()+"].Name",item2.Name)
  @{ index = index +1; } 
} 

您也可以参考这篇文章

只需创建一个新的Model类,如:

public class _ContributeViewModel2
{
  List<PMS.View_Model._ContributeViewModel> ContributeList { get; set; }
  string SearchString { get; set; }
}

将此传递到您的视图并像这样使用,

@model _ContributeViewModel2
@{
    ViewBag.Title = "Contribute";
}
@using (Html.BeginForm())
{
    @Html.Partial("_Project")
    @Html.ValidationSummary(true)
      <table>
        <tr>
              <th>
                  <u>Current Members</u>
              </th>
          </tr>
      <tr>
          <th>
              ProjectID
          </th>
          <th>
              Name
          </th>
          <th>
              Role
          </th>
      </tr>
        @foreach(var item in Model.ContributeList.Where(m=>m.Status==1))
            {
          <tr>
              <td>             
                 @Html.DisplayFor(item.ProjectID)
              </td>
              <td>
                 @Html.DisplayFor(item.Name)
              </td>
              <td>
                 @Html.DisplayFor(item.RoleName)
              </td>
          </tr>
        } 
          <tr>
              <th>
                  <u>Invited Members</u>
              </th>
          </tr>
          <tr>
              <th>
                  ProjectID
              </th>
              <th>
                  Name
              </th>
          </tr>
      @foreach(var item2 in Model.ContributeList.Where(m=>m.Status==0))
            {       
          <tr>             
              <td>
                  @Html.DisplayFor(item2.ProjectID)
              </td>
              <td>
                 @Html.DisplayFor(item2.Name)
              </td>

              </tr>
            } 
          <tr>
              <td>
                  @Html.TextBoxFor(model=> m.SearchString)
              </td>
              <td>
                  <input type="submit" id="btnsearch" value="Search"/>
              </td>
          </tr>
    </table>
}

谢谢。

相关内容

最新更新