c# - 如何使用 asp.net MVC 在 GridView 中编辑数据?

标签 c# asp.net-mvc-3 sql-server-2008

我正在将数据从 SQL Server 绑定(bind)到 gridview,但在编辑时遇到了一些问题。

这是我的模型代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;

namespace Gridview_BugTracker.Models
{
    public class BugTracker_DataHelper
      {

        public static List<BugTracker_DataHelper> GetList{get;set;}

          public string ProjectId { get; set; }
          public string projectName { get; set; }           
          public string Description { get; set; }
           public  string status { get; set; }         
        }     

   }

这是我的 Controller 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using Gridview_BugTracker.Models;
using System.Data.SqlClient;
using System.Data.Entity;

namespace Gridview_BugTracker.Controllers
{
    public class ProjectsController : Controller
    {
        //
        // GET: /Projects/


        public ActionResult Index()
        {
            var bugedlist = GetList(); 
            return View(bugedlist);
        }
        [HttpGet]
        public ActionResult Edit(int projectId)
        {
             BugTracker_DataHelper bugedit = new BugTracker_DataHelper();           
             var edit = EditList();
             bugedit.ProjectId =Convert.ToString(projectId);
             return View(edit);      
        }

        public List<BugTracker_DataHelper> GetList()
        {
            var modelList = new List<BugTracker_DataHelper>();
            using (SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
            {
                conn.Open();
                SqlCommand dCmd = new SqlCommand("Select * from Projects", 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 BugTracker_DataHelper();
                    model.ProjectId = ds.Tables[0].Rows[i]["ProjectId"].ToString();
                    model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
                    model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
                    model.status = ds.Tables[0].Rows[i]["Status"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

     [HttpPost]   
    public ActionResult EditList()
        {
            var editlist = new List<BugTracker_DataHelper>();
            using (SqlConnection editconn=new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
            {
                editconn.Open();
                var modeledit = new BugTracker_DataHelper();
                SqlCommand ecmd = new SqlCommand("EditGetList", editconn);
                ecmd.CommandType = CommandType.StoredProcedure;
                ecmd.Parameters.Add("@projectID", SqlDbType.Int).Value = modeledit.ProjectId;
                Object prjid = ecmd.ExecuteNonQuery();
                editconn.Close();                 

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

GridView 代码

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
    <h2>Index</h2>
<table>
    <tr>         
        <th>
            ProjectName
        </th>

        <th>
           Status
        </th>       

    </tr>

 <% foreach (var item in Model) { %> 

    <tr> 

        <td> 
            <%:Html.LabelForModel(item.projectName) %> 
        </td> 

        <td>
         <%:Html.LabelForModel(item.status) %>
        </td>

         <td>
            <%: Html.ActionLink("Edit", "Edit", new { projectId = item.projectName})%> |
            <%: Html.ActionLink("Details", "Details", new { projectId = item.Description })%> |
            <%: Html.ActionLink("Delete", "Delete", new { projectId = item.status })%>
        </td>

        </tr>

       <%} %>

</table>
    </div>
</body>
</html>

编辑页面代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SiteMaster.Master" Inherits="System.Web.Mvc.ViewPage<Gridview_BugTracker.Models.BugTracker_DataHelper>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    editindex
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>editindex</h2>


    <fieldset>
        <legend>EDit</legend>        

        <div class="editor-label">
           <%:  Html.LabelFor(Model => Model.ProjectId) %>
        </div>
        <div class="editor-field">
           <%: Html.EditorFor(model => model) %>
           <%:  Html.ValidationMessageFor(model => model.projectName) %>
        </div>

        <div class="editor-label">
           <%: Html.LabelFor(model => model.Description) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Description) %>
            <%: Html.ValidationMessageFor(model => model.Description)%>
        </div>


         <div class="editor-label">
           <%:  Html.LabelFor(model => model.status) %>
        </div>
        <div class="editor-field">
          <%:   Html.EditorFor(model => model.status) %>
          <%:    Html.ValidationMessageFor(model => model.status) %>
        </div>   


        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
</asp:Content>

当我点击编辑按钮时,出现以下错误。

“/”应用程序中的服务器错误。

对于“Gridview_BugTracker.Controllers.ProjectsController”中的方法“System.Web.Mvc.ActionResult Edit(Int32)”,参数字典包含不可为空类型“System.Int32”的参数“projectId”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

有人可以帮忙吗?

最佳答案

我相信您的问题可能出在以下行中。

 <%: Html.ActionLink("Edit", "Edit", new { projectId = item.projectName})%>

您的编辑操作需要一个 Int,但您传递给它的是我假设的字符串。所以它永远不会正确绑定(bind),因此您最终会得到一个空的 projectId。

希望这对您有所帮助。

关于c# - 如何使用 asp.net MVC 在 GridView 中编辑数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502615/

相关文章:

c# - 处理 WCF 超时的最佳方法

asp.net-mvc-3 - ASP.NET MVC 自定义错误页面与 Magical Unicorn

sql-server - ZIP 文件在 SSIS 中不起作用(服务器级别问题?)

sql-server - 将 SSIS 包作为 SQLAgent 作业执行

c# - 我们如何使用序列化私有(private)字段?

c# 日期比较返回 DateTime 对象

c# - Web 服务 Excel 转换

c# - 派生抽象类 asp.net mvc 的 JSON 自定义 Binder null

asp.net - 在asp.net mvc中提供动态角色(角色不固定,不断更新)

sql - 如果表中存在列,则重命名该列