2013-05-04 34 views
-1

SQL中的數據綁定是新來的MVC ..我有,我有綁定使用asp.net MVC3(剃刀)的Web電網在現有的SQL表中的數據的任務。 。現在我必須編輯webGrid中的數據..我不知道如何編輯操作將作出... Plzz幫助我...我如何動態編輯使用MVC3網格

我已經給我的綁定數據.. plz讓我知道如何編輯它...

控制器:

public ActionResult Index() 
    { 
     var list = GetList(); 
     return View(list); 
    } 

    public List<Teacher> GetList() 
    { 
     var modelList = new List<Teacher>(); 
     using (SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Demo;Data Source=CIPL41\SQLEXPRESS")) 
     { 
      conn.Open(); 
      SqlCommand dCmd = new SqlCommand("Select T_Id,T_Name,T_Address,Sub_Id from teacher", conn); 
      SqlDataAdapter da = new SqlDataAdapter(dCmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds); 
      conn.Close(); 
      for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
      { 
       var model = new Teacher(); 
       model.T_Id = Convert.ToInt32(ds.Tables[0].Rows[i]["T_Id"]); 
       model.T_Name = ds.Tables[0].Rows[i]["T_Name"].ToString(); 
       model.T_Address = ds.Tables[0].Rows[i]["T_Address"].ToString(); 
       model.Sub_Id = ds.Tables[0].Rows[i]["Sub_Id"].ToString(); 
       modelList.Add(model); 
      } 
     } 
     return modelList; 
    } 
    // 

Index.cshtml

@model IEnumerable<MvcApplication1.Models.Teacher> 

    @{ 
     ViewBag.Title = "Index"; 
     } 

    <h2>Index</h2> 
    @using (Html.BeginForm("Index", "Teacher")) 
     { 

    <table> 
<tr> 
    <th></th> 
    <th> 
     T_Id 
    </th> 
    <th> 
     T_Name 
    </th> 
    <th> 
     T_Address 
    </th> 
    <th> 
     Sub_Id 
    </th> 

</tr> 

@foreach (var item in Model) 
{ 

<tr> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.T_Id }) | 
     @* @Html.ActionLink("Details", "Details", new { id=item.T_Id }) |*@ 
     @Html.ActionLink("Delete", "Delete", new { id=item.T_Id }) 
    </td> 
    <td> 
     @Html.TextBox("T_Id", item.T_Id , new { @style = "width:100px;" }) 
    </td> 
    <td> 
     @Html.TextBox("T_Name", item.T_Name , new { @style = "width:100px;" }) 
     </td> 
    <td> 
     @Html.TextBox("T_Address", item.T_Address , new { @style = "width:100px;" }) 
    </td> 
    <td> 
     @Html.TextBox("Sub_Id",item.Sub_Id,new { @style = "width:100px;"}) 
    </td> 

</tr> 
    } 


    </table> 

PLZ幫我....

回答

0
+0

感謝,但它不正是我想要的。我想編輯從sql的綁定數據..不是在應用程序中新創建的表..他們使用DBContext和Dbset。我希望你明白我想說什麼.. – user2343481 2013-05-04 10:36:21

+0

你是指他們將它保存到數據庫的部分?他們調用db.SaveChanges()的HttpPost? – 2013-05-04 11:31:06