-1

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

我已经给了我的绑定数据..请让我知道如何编辑它...

控制器:

    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;
    }
    //

索引.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>

请帮帮我....

4

1 回答 1

0

看看这个教程http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examineing-the-edit-methods-and-edit-view

我认为它准确地解释了你想要做什么。

于 2013-05-04T08:41:55.223 回答