前台代码如下:
<body>
<div>
<h1>这里是Demo3页面</h1>
<form action="Demo3Index" method="post">
<h1>购买火车票人员名单……</h1>
@{ var i = 1; }
@foreach (Demo1.Models.WB_User item in ViewBag.namelist as List<Demo1.Models.WB_User>)
{
<label>名单 @i:</label><input type="text" id="@item.UserID" name="@i" value="@item.UserAccount" />
<br />
i++;
}
<input id="chanage" type="submit" name="" value="修改" />
</form>
</div>
</body>
后台代码如下:
public ActionResult Demo3Index()
{
using (WB_MicroblogEntities db = new WB_MicroblogEntities())
{
ViewBag.namelist = db.WB_User.ToList();
}
return View();
}
[HttpPost]
public ActionResult Demo3Index(IList<Demo1.Models.WB_User> user)
{
using (WB_MicroblogEntities db = new WB_MicroblogEntities())
{
var userlist = db.WB_User.ToList();
for (int i = 0; i < userlist.Count; i++)
{
// userlist[i].UserID当前项ID
var data = db.WB_User.FirstOrDefault(c=>c.UserID== userlist[i].UserID);
data.UserAccount = userlist[i].UserID;
}
}
return View();
}
代码中加粗有下下划线的其实是小人代码生成的页面input的ID,我想拿值循环修改,该怎么搞,总感觉咱的路子有点野,应该是姿势不对,求指点