视图
@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 =&gt; Model.Name或Something)将文本框值传递给controller ..因为列表?但如果我删除列表&lt;&gt; ..我在 foreach 循环中遇到问题...我应该使用此文本框的部分视图吗?还是有比使用部分观点更好的方法?请建议......
答案 0 :(得分:0)
您可以这样使用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; }
}
您也可以参考this article
答案 1 :(得分:0)
只需创建一个新的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>
}
谢谢。