0

RazorHTMLを使用してMVC4でアプリケーションを作成しました。いくつかのプロパティを持つモデルと、以下に掲載されているdeleteメソッドを設定しました。このメソッドは、プロパティの1つが特定の数でない場合に何も削除できないようにすることを除いて、完全に機能します。このメソッドでそれをどのように実装できますか?

public ActionResult Delete(int id = 0)
{
Material material = db.Materials.Find(id);
if(material == null)
{
    return HttpNotFound();
}
return View(material);
}


[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Material material = db.Materials.Find(id);
db.Materials.Remove(material);
db.SaveChanges();
return RedirecttoAction("Index");
}
4

1 に答える 1

2

getアクションとpostアクションの両方で、プロパティをチェックし、チェックしているプロパティに許容値がない場合は、代替の「削除は許可されていません」ビューを返します。

public ActionResult Delete(int id = 0)
{

    Material material = db.Materials.Find(id);
    if(material == null)
    {
        return HttpNotFound();
    }
    else if (material.CheckedProperty != AllowableValue)
    {
        return View("DeleteNotAllowed", material);
    }
    return View(material);
}


[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
    Material material = db.Materials.Find(id);
    if (material.CheckedProperty != AllowableValue)
    {
         return View("DeleteNotAllowed", material);
    }

    db.Materials.Remove(material);
    db.SaveChanges();
    return RedirecttoAction("Index");
}
于 2013-03-24T22:04:47.430 に答える