0

我正在使用实体框架学习 MVC 3,但我的控制器中的删除操作结果方法存在问题。我有两种用于删除的 ActionResult 方法,一种使用 [HttpPost] 向数据库提交数据,另一种用于使用基于 ID 的正确记录填充视图。

我的问题是我的参数中有一个空对象,所以当我尝试提交删除事务时,它看不到正确的值,因为我有带有空值的空对象。

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 中的品牌参数在对象中有数据,而不是空值。

有谁知道如何解决这一问题?谢谢。

4

2 回答 2

0

我有同样的问题!

我把 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();
        }
    }

查看代码:

@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>
}
于 2012-12-07T18:12:44.800 回答
0

我为解决这个问题所做的就是添加这行代码:

brand = db.Brands.Find(id);

这似乎找到了与我作为 int id 参数传递给 ActionResult 方法的 ID 关联的正确数据。我不明白为什么 Edit actionresult 方法有效,但这不起作用,但这对我来说是一个可以接受的解决方案。

于 2012-10-25T10:14:12.283 回答