我正在从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);
}
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>
当我点击编辑按钮时,我收到以下错误。
参数字典包含参数&#39; projectId&#39;的空条目。非可空类型的System.Int32&#39; for method&#39; System.Web.Mvc.ActionResult Edit(Int32)&#39;在&#39; Gridview_BugTracker.Controllers.ProjectsController&#39;。可选参数必须是引用类型,可空类型,或者声明为可选参数。 参数名称:参数 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
有人可以帮忙吗?
答案 0 :(得分:0)
我相信你的问题可能在以下几行。
<%: Html.ActionLink("Edit", "Edit", new { projectId = item.projectName})%>
您的编辑操作期待一个Int,但是您传递的是我假设的字符串。所以它永远不会正确绑定,因此你最终得到一个null projectId。
希望这有帮助。
答案 1 :(得分:0)
ProjectName is declared as string
public string projectName { get; set; }
在编辑项目时,您应该传递一个int但是传递一个字符串
<%: Html.ActionLink("Edit", "Edit", new { projectId = item.projectName})%>
答案 2 :(得分: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);
}