【发布时间】:2012-03-27 21:28:39
【问题描述】:
所以我刚刚在http://msdn.microsoft.com/en-us/data/gg685489 完成了这个项目。我正在尝试创建一个 CRUD 以与已经存在的外部数据库一起使用,我只是将该项目用作我自己工作的指南。我已经能够更改某些变量名称和连接以连接我正在尝试使用的数据库,我什至使用索引方法显示了我想要的数据。我还能够让 create 方法正常工作,并且能够在我正在使用的数据库中创建新数据。不幸的是,在我进行了我想要的更改后,我无法在使用编辑或删除方法或保存/删除时提取数据。
例如,当我按下编辑按钮时,它应该拉出我单击的记录,以便我可以编辑特定的记录信息,但它抛出了一个错误,指出该变量不能接受空值,这就是我更改int 到编辑删除方法中的字符串。 这部分解决了
我认为这个问题与 Edit 和 Delete 方法在我告诉它时没有提取记录集有关。有谁知道为什么在我告诉它保存后编辑/删除或保存我的更改时它没有拉记录集?
我已经发布了我的 PaController 类,其中包含用于诊断的 CRUD 方法以及我的 iamp_mapping 类文件,其中包含我需要使用的 3 个字段。我 90% 确定我在控制器上做错了,但如果您需要更多关于该问题的代码/信息,请给我留言,因为我被卡住了,我会回来检查很多!非常感谢您的帮助!
PaController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
using System.Data;
namespace DBFirstMVC.Controllers
{
public class PaController : Controller
{
PaEntities db = new PaEntities();
//
// GET: /Pa/
public ActionResult Index()
{
using (var db = new PaEntities())
{
return View(db.iamp_mapping.ToList());
}
}
//
// GET: /Pa/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Pa/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Pa/Create
[HttpPost]
public ActionResult Create(iamp_mapping IAMP)
{
try
{
using (var db = new PaEntities())
{
db.iamp_mapping.Add(IAMP);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Pa/Edit/5
public ActionResult Edit(string id)
{
using (var db = new PaEntities())
{
return View(db.iamp_mapping.Find(id));
}
}
//
// POST: /Pa/Edit/5
[HttpPost]
public ActionResult Edit(string id, iamp_mapping IAMP)
{
try
{
using (var db = new PaEntities())
{
db.Entry(IAMP).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
return View();
}
}
//
// GET: /Pa/Delete/5
public ActionResult Delete(string id)
{
using (var db = new PaEntities())
{
return View(db.iamp_mapping.Find(id));
}
}
//
// POST: /Pa/Delete/5
[HttpPost]
public ActionResult Delete(string id, iamp_mapping IAMP)
{
try
{
using (var db = new PaEntities())
{
db.Entry(IAMP).State = EntityState.Deleted;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
return View();
}
}
}
}
iamp_mapping
using System;
using System.Collections.Generic;
namespace DBFirstMVC.Models
{
public partial class iamp_mapping
{
public string PA { get; set; }
public string MAJOR_PROGRAM { get; set; }
public string INVESTMENT_AREA { get; set; }
}
}
编辑查看代码
@model DBFirstMVC.Models.iamp_mapping
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>iamp_mapping</legend>
<div class="editor-label">
@Html.LabelFor(model => model.PA)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PA)
@Html.ValidationMessageFor(model => model.PA)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MAJOR_PROGRAM)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MAJOR_PROGRAM)
@Html.ValidationMessageFor(model => model.MAJOR_PROGRAM)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.INVESTMENT_AREA)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.INVESTMENT_AREA)
@Html.ValidationMessageFor(model => model.INVESTMENT_AREA)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Index.cshtml
@model IEnumerable<DBFirstMVC.Models.iamp_mapping>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
PA
</th>
<th>
MAJOR PROGRAM
</th>
<th>
INVESTMENT AREA
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.PA)
</td>
<td>
@Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
</td>
<td>
@Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.PA }) |
@Html.ActionLink("Details", "Details", new { id=item.PA }) |
@Html.ActionLink("Delete", "Delete", new { id=item.PA })
</td>
</tr>
}
ActionLink 是我必须解决的问题,当我第一次发布问题时,它看起来像这样
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
我将其更改为实际的主键 PA 并删除了 cmets。这解决了尝试编辑行时数据未填写的问题!
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.PA }) |
@Html.ActionLink("Details", "Details", new { id=item.PA }) |
@Html.ActionLink("Delete", "Delete", new { id=item.PA })
</td>
</tr>
}
删除.cshtml
@model DBFirstMVC.Models.iamp_mapping
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>iamp_mapping</legend>
<div class="display-label">PA</div>
<div class="display-field">
@Html.DisplayFor(model => model.PA)
</div>
<div class="display-label">MAJOR_PROGRAM</div>
<div class="display-field">
@Html.DisplayFor(model => model.MAJOR_PROGRAM)
</div>
<div class="display-label">INVESTMENT_AREA</div>
<div class="display-field">
@Html.DisplayFor(model => model.INVESTMENT_AREA)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace DBFirstMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pa", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
【问题讨论】:
-
当 iamp_mapping 没有 int Id 属性时,
db.iamp_mapping.Find(id)如何工作? -
这是一个很好的问题 Henk 我也想知道,当我尝试修复错误时,我实际上将 id 更改为 IAMP,我应该将其更改为 PA 或 Major_Program 或 Investment_Area 会起作用吗?我认为 id 是一种迭代器,但我可能完全错了!
-
我想我知道 id 来自 Henk,上面我添加了索引类中的代码并解释了我做了哪些更改以及原因。
标签: c# asp.net database asp.net-mvc-3 razor