本文介绍了如何在MVC 2中的现有网格中添加“添加/编辑/删除”功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下是我创建的用于显示教师详细信息的页面: grid.aspx [VIEW] Teachers.cs [MODEL ] HomeController.cs [控制器] Teachers.cs [MODEL] 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Data.SqlClient; 使用System.Data; 名称空间MvcApplication1.Models { 公共类教师 { private string _TeacherName ; private int _TeacherID,_StdID; private String ConString = @Data Source = wtpl-et-0009 \; Initial Catalog = Demo; User ID = sa;密码= visual123; 数据集ds; 公共字符串TeacherName { set {_TeacherName = value; } get {return _TeacherName; } } public int TeacherID { set {_TeacherID = value; } get {return _TeacherID; } } public int StdID { set {_StdID = value; } get {return _StdID; } } public DataTable GetData() { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter(select TeacherId,TeacherName,StandardId from Teacher,con); 试试 { sda.Fill(ds); 返回ds.Tables [0]; } catch { 返回null; } } } HomeController.cs [CONTROLLER] 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System .Web; 使用System.Web.Mvc; 使用System.Data; 名称空间MvcApplicati on1.Controllers { 公共类HomeController:控制器 { // // GET:/ Home / public ActionResult AddRecipie() { DataTable dt = new DataTable(); MvcApplication1.Models.Teachers obj = new MvcApplication1.Models.Teachers(); dt = obj.GetData(); 列表< mvcapplication1.models.teachers>记录=新列表< models.teachers>(); foreach(DataRow dr in dt.Rows) { Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dr [TeacherId]); Teacher.TeacherName = dr [TeacherName]。ToString(); Teacher.StdID = Convert.ToInt32(dr [StandardID]。ToString()); Records.Add(Teacher); } 返回查看(AddRecipie,记录); } 公共ActionResult索引() { 返回查看(); } 公共ActionResult网格() { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetData(); 列表< mvcapplication1.models.teachers>记录=新列表< models.teachers>(); foreach(dt.Rows中的DataRow dr) { Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dr [TeacherId]); 老师。 TeacherName = dr [TeacherName]。ToString(); Teacher.StdID = Convert.ToInt32(dr [StandardID]。ToString()); 记录.Add(老师); } 返回查看(记录); } } } grid.aspx [查看] <%@ Page 标题 = 语言 = C# MasterPageFile = 〜/ Views / Shared / ViewMasterPage1.Master 继承 = System.Web.Mvc.ViewPage< ; IEnumerable< MvcApplication1.Models.Teachers>> %> < asp:内容 ID = Content1 续entPlaceHolderID = TitleContent runat = server > 网格 < / asp:内容 > < asp:内容 ID = Content2 ContentPlaceHolderID = 主要内容 runat = 服务器 > < 表格 id = form1 runat = server > < h2 > 网格< / h2 > < asp:GridView ID = GridView1 runat = server AutoGenerateColumns = 错误 DataKeyNames = TeacherId DataSourceID = SqlDataSource1 > < 列s > < asp:BoundField DataField = TeacherId HeaderText = TeacherId InsertVisible = 错误 ReadOnly = True SortExpression = TeacherId / > < asp:BoundField DataField = TeacherName HeaderText = TeacherName SortExpression = TeacherName / > < asp:BoundField DataField = StandardId HeaderText = StandardId SortExpression = StandardId / > < / Columns > < / asp:GridView > < asp: SqlDataSource ID = SqlDataSource1 runat = server ConnectionString = <% $ ConnectionStrings:TestSanketConnectionString %> SelectCommand = SELECT [TeacherId],[TeacherName],[Stand ardId] FROM [Teacher] > < / asp:SqlDataSource > < 表 style = width:50%; > < tr > < td > TeacherId < / td > < td &克t; 教师姓名 < / td > < td > 标准ID < / td > < / tr > <% foreach ( var item in Model) {%> < tr > < TD > <% = Html.DisplayFor(x => item.TeacherID)%> < / td > < td > <% = Html.DisplayFor(x => item .TeacherName)%> < / td > < td > <% = Html.DisplayFor(x => item.StdID)%> < / td > < / tr > <%} %> < / table > < / form > < / asp:Content > 我希望为每一行添加一个按钮进行编辑和删除。 底部有一个按钮,用于添加新记录,然后创建新页面并创建新纪录。 Plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz帮助!!!!!!!!!!! 决方案 ConnectionStrings:TestSanketConnectionString %> SelectCommand = SELECT [TeacherId],[TeacherName],[StandardId] FROM [Teacher] > < / asp:SqlDataSource > < table 样式 = width: 50%; & gt; < tr > < td > TeacherId < / td > < td > 教师姓名 < / td > < td > 标准ID < / td > < / tr > <% foreach ( var item in Model) {%> < tr > < td > <% = Html.DisplayFor(x => item.TeacherID)%> < / td > < td > <% = Html .Disp layFor(x => item.TeacherName)%> < / td > < td > <% = Html.DisplayFor(x => item.StdID)%> < / td > < / tr > < ;%} %> < / table > < / form > < / asp:内容 > 我希望为每一行添加一个按钮进行编辑和删除。 底部有一个按钮,用于添加新记录到新页面并创建新的记录。 Plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz帮助!!!!!!!!!!! @ {布局= null; } < html> < head> < title> Index< / title> < script src =http://maps.google.com/maps/api/js?sensor=false>< ; / script> < script type =text / javascriptsrc =http://www.google.com/jsapi>< / script> < script type =text / javascriptsrc =../../ Scripts / jquery-1.4.4.js>< / script> < script type =text / javascriptsrc =../../ Scripts / jquery-ui.js>< / script> < script type = text / javascript> google.load(elements,1,{packages:transliteration}); < / script> < script> 函数OnLoad(){ var currValue = document.getElementById(text1); var options = { sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH, destinationLanguage: [google.elements.transliteration.LanguageCode.HINDI], shortcutKey:'ctrl + g', transliterationEnabled:true }; var control = new google.elements.transliteration.TransliterationControl(options); control.makeTransliteratable([text1]); var postValue = document.getElementById( text1); } google.setOnLoadCallback(OnLoad); < / script> < script type =text / javascript> 函数执行(urlString,userData){ var result; .ajax({ cache:false, 类型:POST,// imp async:false, url:urlString, contentType:'application / json; charset = utf-8', dataType:json, 数据:userData, 成功:函数(数据){ 结果=数据; 返回结果; }, 错误:函数(req,status,error){ document.write(req.responseText); } }); 返回结果; } Here are the pages i have created to show Teacher's Details:grid.aspx [VIEW]Teachers.cs [MODEL]HomeController.cs [CONTROLLER]Teachers.cs [MODEL]using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.SqlClient;using System.Data;namespace MvcApplication1.Models{ public class Teachers { private string _TeacherName; private int _TeacherID, _StdID; private String ConString = @"Data Source=wtpl-et-0009\;Initial Catalog=Demo;User ID=sa;Password=visual123"; DataSet ds; public string TeacherName { set { _TeacherName = value; } get { return _TeacherName; } } public int TeacherID { set { _TeacherID = value; } get { return _TeacherID; } } public int StdID { set { _StdID = value; } get { return _StdID; } } public DataTable GetData() { ds = new DataSet(); SqlConnection con = new SqlConnection(ConString); SqlDataAdapter sda = new SqlDataAdapter("select TeacherId,TeacherName,StandardId from Teacher",con); try { sda.Fill(ds); return ds.Tables[0]; } catch { return null; } } }HomeController.cs [CONTROLLER]using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;namespace MvcApplication1.Controllers{ public class HomeController : Controller { // // GET: /Home/ public ActionResult AddRecipie() { DataTable dt=new DataTable(); MvcApplication1.Models.Teachers obj = new MvcApplication1.Models.Teachers(); dt = obj.GetData(); List<mvcapplication1.models.teachers> Records=new List<models.teachers>(); foreach(DataRow dr in dt.Rows) { Models.Teachers Teacher=new Models.Teachers(); Teacher.TeacherID=Convert.ToInt32(dr["TeacherId"]); Teacher.TeacherName=dr["TeacherName"].ToString(); Teacher.StdID=Convert.ToInt32(dr["StandardID"].ToString()); Records.Add(Teacher); } return View("AddRecipie",Records); } public ActionResult Index() { return View(); } public ActionResult Grid() { DataTable dt = new DataTable(); Models.Teachers obj = new Models.Teachers(); dt = obj.GetData(); List<mvcapplication1.models.teachers> Records = new List<models.teachers>(); foreach (DataRow dr in dt.Rows) { Models.Teachers Teacher = new Models.Teachers(); Teacher.TeacherID = Convert.ToInt32(dr["TeacherId"]); Teacher.TeacherName = dr["TeacherName"].ToString(); Teacher.StdID = Convert.ToInt32(dr["StandardID"].ToString()); Records.Add(Teacher); } return View(Records); } }}grid.aspx [VIEW]<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage1.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Teachers>>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Grid</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <form id="form1" runat="server"> <h2>Grid</h2> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="TeacherId" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="TeacherId" HeaderText="TeacherId" InsertVisible="False" ReadOnly="True" SortExpression="TeacherId" /> <asp:BoundField DataField="TeacherName" HeaderText="TeacherName" SortExpression="TeacherName" /> <asp:BoundField DataField="StandardId" HeaderText="StandardId" SortExpression="StandardId" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestSanketConnectionString %>" SelectCommand="SELECT [TeacherId], [TeacherName], [StandardId] FROM [Teacher]"></asp:SqlDataSource> <table style="width:50%;"><tr><td>TeacherId</td><td>Teacher Name</td><td>Standard Id</td></tr><%foreach(var item in Model) { %> <tr> <td> <%=Html.DisplayFor(x=>item.TeacherID)%> </td> <td> <%=Html.DisplayFor(x=>item.TeacherName)%> </td> <td> <%=Html.DisplayFor(x=>item.StdID)%> </td> </tr> <% } %></table></form></asp:Content>I wish to add a button for each row for editing and deleting.One button at the bottom for addition of new record which takes to a new page and creates new records.Plzzzzzzzzzzzzzzzzzzzzzzzzzz Help!!!!!!!!!!! 解决方案 ConnectionStrings:TestSanketConnectionString %>" SelectCommand="SELECT [TeacherId], [TeacherName], [StandardId] FROM [Teacher]"></asp:SqlDataSource> <table style="width:50%;"><tr><td>TeacherId</td><td>Teacher Name</td><td>Standard Id</td></tr><%foreach(var item in Model) { %> <tr> <td> <%=Html.DisplayFor(x=>item.TeacherID)%> </td> <td> <%=Html.DisplayFor(x=>item.TeacherName)%> </td> <td> <%=Html.DisplayFor(x=>item.StdID)%> </td> </tr> <% } %></table></form></asp:Content>I wish to add a button for each row for editing and deleting.One button at the bottom for addition of new record which takes to a new page and creates new records.Plzzzzzzzzzzzzzzzzzzzzzzzzzz Help!!!!!!!!!!!@{ Layout = null;}<html><head> <title>Index</title> <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript" src="../../Scripts/jquery-1.4.4.js"></script> <script type="text/javascript" src="../../Scripts/jquery-ui.js"></script> <script type="text/javascript"> google.load("elements", "1", { packages: "transliteration" }); </script> <script> function OnLoad() { var currValue = document.getElementById("text1"); var options = { sourceLanguage:google.elements.transliteration.LanguageCode.ENGLISH, destinationLanguage:[google.elements.transliteration.LanguageCode.HINDI], shortcutKey: 'ctrl+g', transliterationEnabled: true }; var control = new google.elements.transliteration.TransliterationControl(options); control.makeTransliteratable(["text1"]); var postValue = document.getElementById("text1"); } google.setOnLoadCallback(OnLoad); </script> <script type="text/javascript"> function Execute(urlString, userData) { var result;.ajax({ cache: false, type: "POST", //imp async: false, url: urlString, contentType: 'application/json; charset=utf-8', dataType: "json", data: userData, success: function (data) { result = data; return result; }, error: function (req, status, error) { document.write(req.responseText); } }); return result; } 这篇关于如何在MVC 2中的现有网格中添加“添加/编辑/删除”功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 23:23