jquery - Mvc4中的WebGrid控件问题

标签 jquery asp.net-mvc-4 razor checkbox webgrid

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

家庭 Controller

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);
        }

Student.cs

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

Index.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将显示在隐藏字段中,Name将显示在Textbox 和数据加载时 checkbox 将检查其属性是否具有 true 值,checkbox 将检查,如果属性具有 false 值,则检查 checkbox 取消选中

拜托拜托请帮帮我!

任何帮助将不胜感激!

最佳答案

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

@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>)
 })

关于jquery - Mvc4中的WebGrid控件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832739/

相关文章:

c# - 迭代 IEnumerable 中的每个项目

jquery - Controller 可以根据结果返回 JSON 或 HTML

javascript - 在提交到服务器 MVC 之前创建一个预览页面

ASP.NET MVC5 : Unobtrusive validation during the filling out of the form field

javascript - Jquery模态窗口仅在输入值长度大于2时显示

javascript - 删除元素后,在 JQuery 的 .each 循环中,索引每隔三分之一跳一次。

javascript - 选择类时 jQuery 选择器速度较慢

c# - VirtualPathProvider 等同于 ASP.NET 5/MVC6?

c# - 将 Viewmodel 对象发送到 ASP .Net MVC 中的 Razor View

android - 使溢出滚动条在 iOS 和 Android 移动设备上可见