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

问题描述 投票:2回答:3

我正在将数据从SQL Server绑定到gridview,但是在Edit上存在一些问题。

这是我的型号代码:

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

   }

这是我的控制器代码

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)'的非null类型'System.Int32'的参数'projectId'包含一个空条目。可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。参数名称:参数说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

任何人都可以帮忙吗?

c# asp.net-mvc-3 sql-server-2008
3个回答
0
投票

我相信您的问题可能在下一行。

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

您的编辑操作期望一个Int,但是您正在传递它,我假设它是字符串。因此它永远不会正确绑定,因此您最终会得到一个空的projectId。

希望这会有所帮助。


0
投票
ProjectName is declared as string
      public string projectName { get; set; }  

在编辑项目时,您应该传递一个int但您传递一个字符串

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

0
投票
public ActionResult Edit(int projectId)
{
     BugTracker_DataHelper bugedit = new BugTracker_DataHelper();           
     var edit = EditList();
     bugedit.ProjectId =Convert.ToString(projectId);
     return View(edit);      
}

我认为您的问题出在方法上,该方法应重写如下:

public ActionResult Edit(int projectId)
{
     BugTracker_DataHelper bugedit = new BugTracker_DataHelper();           
     bugedit.ProjectId =Convert.ToString(projectId);
     var edit = EditList();
     return View(edit);      
}
© www.soinside.com 2019 - 2024. All rights reserved.