如何使用asp.net MVC在GridView中编辑绑定数据?

时间:2013-05-02 14:37:19

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

我在asp.net mvc3中使用网格绑定数据。我在绑定表中的编辑链接有问题。如果我点击编辑链接,它没有响应。这就是我所拥有的:

Teacher.cs

public class Teacher
{
    public static List<Teacher> GetList { get; set; }

    public int  T_Id { get; set; }
    public string T_Name { get; set; }
    public string T_Address { get; set; }
    public string Sub_Id { get; set; }  


}

Teachercontroller

public ActionResult Edit(int id,string T_Name,string T_Address,string Sub_Id)
{
    // Teacher list= new Teacher();
    var edit = EditList();
    //list.T_Id = Convert.ToInt32(T_Id);
    return View(edit);   
}

[HttpPost]
public ActionResult EditList()
{

    var editlist = new List<Teacher>();
    using (SqlConnection conn = new SqlConnection(@"Integrated ecurity=SSPI;Persist Security Info=False;Initial Catalog=Demo;Data Source=CIPL41\SQLEXPRESS"))
    {

        conn.Open();
        var modeledit = new Teacher();
        SqlCommand cmd = new SqlCommand("edit", conn);
        cmd.CommandType = CommandType.StoredProcedure;               
        cmd.Parameters.AddWithValue("@T_Id", modeledit.T_Id);
        cmd.Parameters.AddWithValue("@T_Name", modeledit.T_Name);
        cmd.Parameters.AddWithValue("@T_Address", modeledit.T_Address);
        cmd.Parameters.AddWithValue("@Sub_Id", modeledit.Sub_Id);
        cmd.ExecuteNonQuery();
        conn.Close();enter code here

        editlist.Add(modeledit);
    }
    return View(editlist);
}

1 个答案:

答案 0 :(得分:0)

这是我的Html代码:

Index.cshtml:

@model IEnumerable<MvcApplication1.Models.Teacher>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
<p>

    @Html.ActionLink("Create New", "Create")

</p>
<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>
        @item.T_Id
    </td>
    <td>
        @item.T_Name
    </td>
    <td>
        @item.T_Address
    </td>
    <td>
        @item.Sub_Id
    </td>
</tr>

}

 **Edit.cshtml:**




@model MvcApplication1.Models.Teacher

@{
ViewBag.Title = "Edit";
}   

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">     </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Teacher</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.T_Id)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.T_Id)
        @Html.ValidationMessageFor(model => model.T_Id)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.T_Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.T_Name)
        @Html.ValidationMessageFor(model => model.T_Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.T_Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.T_Address)
        @Html.ValidationMessageFor(model => model.T_Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Sub_Id)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Sub_Id)
        @Html.ValidationMessageFor(model => model.Sub_Id)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Index")