2013-09-16 42 views
2

我在MVC新,我試圖用WebGird控制在我的演示應用程序,我做一些任務象下面這樣: -的WebGrid控制問題Mvc4

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

學生的.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模式中的所有數據,但我需要這些數據將在編輯模式如身份證顯示將隱藏字段顯示,名稱會顯示在Textbox,當數據加載checkbox將檢查如果財產有真實價值checkbox將檢查,如果財產有虛假價值checkboxuncheck

請請幫幫我!

任何幫助將不勝感激!

回答

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

非常感謝主席先生! –

+1

不客氣。我很高興你發現我的答案有幫助。 –

+0

主席先生,我發現了一個與 Web網格問題有關的新問題,我想在按鈕單擊時添加一個新行(動態)。 –