公共ActionResult删除(int id,品牌商标) - Null品牌对象(MVC3& Entity Framework)

时间:2012-10-25 09:15:31

标签: asp.net-mvc-3 entity-framework-4.1 null-object-pattern

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

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

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

这是带有null对象的代码:

 [HttpPost]
 public ActionResult Delete(int id, Brand brand)

品牌是空的,我不明白为什么。例如

[HttpPost]
public ActionResult Edit(int id, Brand brand)

Edit actionresult中的品牌参数在对象中包含数据,而不是空值。

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

2 个答案:

答案 0 :(得分:0)

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

brand = db.Brands.Find(id);

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

答案 1 :(得分:0)

我遇到了同样的问题!

我把代码行放在第一位:

[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>
}