2013-05-04 50 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