如何使用mvc3 web grid动态编辑sql中的绑定数据

时间:2013-05-04 08:08:29

标签: asp.net-mvc-3 binding datagrid edit

我是mvc的新手..我有一个任务,我必须使用asp.net mvc3(Razor)Web Grid绑定sql中现有表的数据..现在我必须编辑webGrid中的数据..我不知道编辑操作将如何进行... Plzz帮帮我......

我已经提供了绑定数据..请告诉我如何编辑它...

控制器:

    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帮助我......

1 个答案:

答案 0 :(得分:0)