سؤال

I am new in MVC and I'm trying to use WebGird control in my demo application I do some task like below:-

HomeController

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

Above WebGrid will show all data in readonly mode, but I need those data will show in editable mode such as Id will show in Hidden field, Name will shows in Textbox and when data load checkbox will checked if its property have true value the checkbox will checked and if property have false value then checkbox will uncheck.

Please Please Please Help me!

Any help will be appreciated!

هل كانت مفيدة؟

المحلول

The following code is my idea about your question

@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>)
 })
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top