2013-05-02 60 views
0

我在asp.net mvc3中使用網格綁定數據。我在綁定表中的編輯鏈接有問題。如果我點擊編輯鏈接,它沒有迴應。下面是我有:如何使用asp.net MVC在GridView中編輯綁定數據?

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); 
} 
+0

請告訴我們您的HTML。 – 2013-05-02 14:41:30

+0

我已經在答案中顯示了我的Html .. plz檢查它 – user2343481 2013-05-03 05:53:50

回答

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") 

相關問題