簡體   English   中英

如何使用 asp.net MVC 在 GridView 中編輯數據?

[英]How to edit data in GridView using asp.net MVC?

我正在將數據從 SQL 服務器綁定到 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; }         
        }     

   }

這是我的控制器代碼

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

網格視圖代碼

 <%@ 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”的空條目。 可選參數必須是引用類型、可空類型或聲明為可選參數。 參數名稱:parameters 描述:當前web請求執行過程中發生了未處理的異常。 請查看堆棧跟蹤以獲取有關錯誤及其源自代碼的位置的更多信息。

有人可以幫忙嗎?

我相信您的問題可能在以下行。

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

您的編輯操作需要一個 Int 但您正在傳遞它,我假設它是一個字符串。 所以它永遠不會正確綁定,因此你最終會得到一個 null projectId。

希望這可以幫助。

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

在編輯項目時,您應該傳遞一個 int 但您傳遞的是一個字符串

<%: Html.ActionLink("Edit", "Edit", new { projectId = item.projectName})%>
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);      
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM