Asp.net mvc 3 Public ActionResult Delete(int-id,brand-brand)-空品牌对象(MVC3和实体框架)
我正在学习使用实体框架的MVC 3,我的控制器中的删除操作结果方法有问题。我有两种用于删除的ActionResult方法,一种是使用[HttpPost]将数据提交到数据库,另一种是使用基于ID的正确记录填充视图 我的问题是我的参数中有一个null对象,所以当我尝试提交一个delete事务时,它看不到正确的值,因为我有一个null对象和null值Asp.net mvc 3 Public ActionResult Delete(int-id,brand-brand)-空品牌对象(MVC3和实体框架),asp.net-mvc-3,entity-framework-4.1,null-object-pattern,Asp.net Mvc 3,Entity Framework 4.1,Null Object Pattern,我正在学习使用实体框架的MVC 3,我的控制器中的删除操作结果方法有问题。我有两种用于删除的ActionResult方法,一种是使用[HttpPost]将数据提交到数据库,另一种是使用基于ID的正确记录填充视图 我的问题是我的参数中有一个null对象,所以当我尝试提交一个delete事务时,它看不到正确的值,因为我有一个null对象和null值 using System; using System.Collections.Generic; using System.Linq; using Sys
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
namespace DBFirstMVC.Controllers
{
public class BrandController : Controller
{
// Instantiate model of my database when controller is called
//TestBradEntities db = new DBFirstMVC.Models.TestBradEntities();
//
// GET: /Brand/
public ActionResult Index()
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.ToList());
}
}
//
// GET: /Brand/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Brand/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Brand/Create
[HttpPost]
public ActionResult Create(Brand brand)
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Brands.Add(brand);
db.SaveChanges();
}
return RedirectToAction("Index");
}
//
// GET: /Brand/Edit/5
public ActionResult Edit(int id)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.Find(id));
}
}
catch (Exception ex)
{
return View(ex.ToString());
}
}
//
// POST: /Brand/Edit/5
[HttpPost]
public ActionResult Edit(int id, Brand brand)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Entry(brand).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception ex)
{
return View();
}
}
//
// GET: /Brand/Delete/5
public ActionResult Delete(int id)
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.Find(id));
}
}
//
// POST: /Brand/Delete/5
[HttpPost]
public ActionResult Delete(int id, Brand brand)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Entry(brand).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
这是具有空对象的代码:
[HttpPost]
public ActionResult Delete(int id, Brand brand)
品牌是空的,我不明白为什么。比如说
[HttpPost]
public ActionResult Edit(int id, Brand brand)
编辑actionresult中的Brand参数在对象中有数据,且不为空值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
namespace DBFirstMVC.Controllers
{
public class BrandController : Controller
{
// Instantiate model of my database when controller is called
//TestBradEntities db = new DBFirstMVC.Models.TestBradEntities();
//
// GET: /Brand/
public ActionResult Index()
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.ToList());
}
}
//
// GET: /Brand/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Brand/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Brand/Create
[HttpPost]
public ActionResult Create(Brand brand)
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Brands.Add(brand);
db.SaveChanges();
}
return RedirectToAction("Index");
}
//
// GET: /Brand/Edit/5
public ActionResult Edit(int id)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.Find(id));
}
}
catch (Exception ex)
{
return View(ex.ToString());
}
}
//
// POST: /Brand/Edit/5
[HttpPost]
public ActionResult Edit(int id, Brand brand)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Entry(brand).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception ex)
{
return View();
}
}
//
// GET: /Brand/Delete/5
public ActionResult Delete(int id)
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
return View(db.Brands.Find(id));
}
}
//
// POST: /Brand/Delete/5
[HttpPost]
public ActionResult Delete(int id, Brand brand)
{
try
{
using (var db = new DBFirstMVC.Models.TestBradEntities1())
{
db.Entry(brand).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
有人知道如何解决这个问题吗?谢谢。我为解决此问题所做的是添加以下代码行:
brand = db.Brands.Find(id);
这似乎找到了与作为int ID参数传递给ActionResult方法的ID相关联的正确数据。我不明白为什么编辑actionresult方法有效,但这不起作用,但这是我可以接受的解决方案。我也有同样的问题 我将find的代码行放在第一位:
[HttpPost]
public ActionResult Delete(int id, Student student)
{
try
{
using (var schoolClient = new SchoolSrvManagerClient())
{
student = schoolClient.GetStudent(id);
student.MarkAsDeleted<Student>();
schoolClient.UpdateStudent(student);
}
return RedirectToAction("ShowStudents");
}
catch (Exception)
{
return View();
}
}
[HttpPost]
公共行动结果删除(int id,学生)
{
尝试
{
使用(var schoolClient=new SchoolSrvManagerClient())
{
student=schoolClient.GetStudent(id);
student.MarkAsDeleted();
schoolClient.UpdateStudent(学生);
}
返回重定向到操作(“ShowStudents”);
}
捕获(例外)
{
返回视图();
}
}
查看代码:
@model SchoolSTEModel.Student
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Student</legend>
<div class="display-label">StudentName</div>
<div class="display-field">
@Html.DisplayFor(model => model.StudentName)
</div>
<div class="display-label">StandardId</div>
<div class="display-field">
@Html.DisplayFor(model => model.StandardId)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
@model SchoolSTEModel.Student
@{
ViewBag.Title=“删除”;
}
删除
是否确实要删除此项?
学生
学生姓名
@DisplayFor(model=>model.StudentName)
标准ID
@DisplayFor(model=>model.StandardId)
@使用(Html.BeginForm()){
|
@ActionLink(“返回列表”、“索引”)
}
你不应该这样做。向控制器发送post时,您可能没有绑定数据。如果您仍然感兴趣,发布您的删除视图。您可能希望添加一些详细信息(或注释),而不仅仅是一堆代码。