2

我是 MVC 的新手,我正在尝试在我的演示应用程序中使用 WebGird 控件,我执行以下任务:-

家庭控制器

public ActionResult Index()
        {
            List<Student> listStudent = new List<Student>();
            listStudent.Add(new Student { Id = 1000, Name = "Sumit Kesarwani", IsActive = true });
            listStudent.Add(new Student { Id = 1001, Name = "Arun Singh", IsActive = true });
            listStudent.Add(new Student { Id = 1002, Name = "Vijay Shukla", IsActive = false });
            listStudent.Add(new Student { Id = 1003, Name = "Pwan Shukla", IsActive = true });
            var data = listStudent;

            return View(data);
        }

学生.cs

public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsActive { get; set; }
    }

索引.cshtml

@model IEnumerable<MvcWebGrid.Models.Student>

@{
    ViewBag.Title = "Home Page";
    WebGrid webGrid = new WebGrid(Model);
}
@webGrid.GetHtml(columns: new[]{
  webGrid.Column("Id"),
  webGrid.Column("Name"),
  webGrid.Column("IsActive", header: "", format:@<text><input name="isActive" 
      type="checkbox" @item.IsActive == "true" ? "checked" : ""/></text>)
})

上面WebGrid将显示readonly模式下的所有数据,但我需要这些数据以可编辑模式显示,例如 Id 将显示在隐藏字段中,名称将显示在Textbox,当数据加载checkbox将检查其属性是否具有真实值时,checkbox将检查属性是否具有假值然后checkboxuncheck

请请请帮帮我!

任何帮助将不胜感激!

4

1 回答 1

3

以下代码是我对您的问题的想法

@model IEnumerable<MvcWebGrid.Models.Student>

@{
    ViewBag.Title = "Home Page";
    var webGrid = new WebGrid(Model);
    Func<bool, MvcHtmlString> func = 
    (b) => b ? MvcHtmlString.Create("checked=\"checked\"") : MvcHtmlString.Empty;
}

@webGrid.GetHtml(columns: new[]{
  webGrid.Column("Id" ,header: "", format:
  @<text>
      <input name="id" type="hidden" value="@item.Id" />
  </text>),
  webGrid.Column("Name", header: "", format:
  @<text>
      <input name="name" type="text" value="@item.Name"/>
  </text>),
   webGrid.Column("IsActive", header: "", format:
   @<text>
       <input name="isActive" type="checkbox" @func(item.IsActive)/>
   </text>)
 })
于 2013-09-16T19:02:44.543 回答