[英]ASP.net MVC 3 Edit/Delete issue
所以我刚刚在http://msdn.microsoft.com/en-us/data/gg685489完成了这个项目。 我正在尝试创建一个 CRUD 以与已经存在的外部数据库一起使用,我只是将该项目用作我自己工作的指南。 我已经能够更改某些变量名称和连接以连接我正在尝试使用的数据库,我什至使用 index 方法显示了我想要的数据。 我还能够使用 create 方法,并且能够在我正在使用的数据库中创建新数据。 不幸的是,在我进行了所需的更改后,我无法在使用编辑或删除方法或保存/删除时提取数据。
例如,当我按下编辑按钮时,它应该拉出我点击的记录,以便我可以编辑特定的记录信息,但它抛出了一个错误,说这个变量不能接受空值,这就是为什么我将 int 更改为 a字符串在编辑删除方法中。 这部分解决了
我认为这个问题与 Edit 和 Delete 方法有关,当我告诉它时没有拉出记录集。 有没有人知道为什么在我告诉它保存后编辑/删除或保存更改时它不提取记录集?
我已经发布了我的 PaController 类,其中包含用于诊断的 CRUD 方法以及我的 iamp_mapping 类文件,其中包含我需要使用的 3 个字段。 我 90% 确定我的控制器有问题,但是如果您需要有关该问题的更多代码/信息,请给我留言,我会经常回来查看,因为我被卡住了! 非常感谢你的帮助!
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();
}
}
}
}
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>
@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 并删除了注释。 这修复了尝试编辑行时未填写数据的问题!
<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>
}
@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>
}
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);
}
}
}
由于某种原因,我的链接无法获取 ID,因此我从一门课程中找到了解决方案。 他像这样在函数中添加了第五个参数“null”
<td>@Html.ActionLink("Edit", "Edit", "Account", new {id = account.Id }, null)</td>
(text, action, controller, route value, null or HTML atribute)这为我修好了。
您的问题出现在这里,当您单击编辑按钮/链接时,编辑操作将需要一个 int 类型的 id(如果您使用 int),但是如果您使用 int,这可以切换为可选?...这就是MVC 的行为......你最好的选择是尝试使用一个 url 说类似 http://urwebsite.com/Pa/Edit?id=[use a valid id]...这样你就会有一个 id编辑动作...
更新
我认为您在这里遇到的问题是 Edit 操作方法中的 Id 值为空...并且 MVC 引擎无法从您的 Url 模式中识别 id 值
因此,您可以使用具有编辑链接/按钮的页面
<a href="/Pa/Edit/@model.id">Edit</a>
如果您有一个用于显示记录集合的 for 循环...
@foreach(var item in Model)
{
<a href="/Pa/Edit/@item.id">Edit</a>
}
还要确保您为 url 模式配置了路由 /Pa/Edit/{id}
如果你想了解更多关于 mvc 中的路由,你可能想阅读这篇文章。
这将确保当对编辑操作方法发出 Get 请求时,将识别 id 值
希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.