mathieuxiaonew 2016-08-19 12:57 采纳率: 50%
浏览 1567
已结题

关于MVC中Html.TextBox的疑问

public class Student
{
public string Name { get; set; }
public string Id { get; set; }

public static List<Student> GetStudents()
{
    List<Student> stus = new List<Student>();
    stus.Add(new Student() { Id = "1", Name = "张三" });
    stus.Add(new Student() { Id = "2", Name = "李四" });

    return stus;
}

}

namespace BookShopUI.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
return View("Index", Student.GetStudents());
}

    [HttpPost]
    public ActionResult Index(List<Student> students)
    {
        students.ForEach(s => s.Id = string.Format("{0}{0}", s.Id));
        return View("Index", students);
    }
}

}

<div>
    <% using (Html.BeginForm())
       { %>
    <% for (int i = 0; i < Model.Count; i++)
       { %>
    <br />
        <%= Html.TextBox("students[" + i + "].Id", Model[i].Id)%> : 
    <%= Html.TextBox("students[" + i + "].Name", Model[i].Name)%>
    <p></p>
    <%}
      %>
    <input type="submit" value="全部提交"/>
    <%
       } %>
</div>

    为了学习mvc的集合数据绑定,做个一个小例子。在第一次提交页面后,文本框中能够显示“1   张三”,修改为“11 张三”,提交。期待出现“1111 张三”的结果,但是页面却没有任何变化。
     如果这里不用Html.TextBox(),而是直接使用<input type="text" value ="<%= Model[i].Id %>" />的话,就能出现希望的“1111”,不知道是什么原因,求解释,谢谢!
  • 写回答

2条回答 默认 最新

  • devmiao 2016-08-19 16:09
    关注

    用Html.TextBoxFor()看看

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据