0

Entity Framework で MVC 3 を学習していますが、コントローラーの削除アクションの結果メソッドに問題があります。削除用の 2 つの ActionResult メソッドがあります。1 つは [HttpPost] を使用してデータをデータベースに送信し、もう 1 つは ID に基づいて正しいレコードをビューに入力します。

私の問題は、パラメーターに null オブジェクトがあるため、削除トランザクションをコミットしようとすると、null 値を持つ 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 の Brand Parameter には、オブジェクトにデータがあり、null 値ではありません。

これを修正する方法を知っている人はいますか?ありがとう。

4

2 に答える 2

0

この問題を解決するために私がしたことは、次のコード行を追加することでした。

brand = db.Brands.Find(id);

これにより、ActionResultメソッドにintidパラメーターとして渡したIDに関連付けられた正しいデータが見つかるようです。Edit actionresultメソッドが機能する理由がわかりませんが、これは機能しませんが、これは私にとって許容できる解決策です。

于 2012-10-25T10:14:12.283 に答える
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 に答える