0

Index アクションを使用して、グリッドのような構造でレコードのリストを表示します。ユーザーが特定の行/レコードを削除できるようにする列の 1 つとして [削除] ボタンもあります。これはうまくいきます。また、個々のレコードを表示できるように、各行に [詳細] リンクもあります。

Delete には独自の HttpPost アクションがあります。詳細には、独自の通常のアクションもあります。

問題は、この [削除] ボタン コードを [詳細] ビューに追加したいのですが、通知機能を使用していて、削除自体は機能しますが、コードには通知機能が表示されます (詳細アクションに record==null チェックがあるため)。それを回避する方法がわかりません。

コードは次のとおりです。

public ActionResult Index()
{
    ...
    var myList = _repository.Table;
    // Nothing else relevant just displays list and sets up model
    return View(model);
}

public ActionResult Details(int id)
{
    ...
    var record = _repository.Get(id);

    // If I use the Delete action below then this will get called and fire;
    // I am trying to figure out how to avoid it firing when I use the Delete code
    // in the Details view (see .cshtml code below)
    if (record == null)
    {
    _myServices.Notifier.Warning
            (T("Request not found, please check the URL."));

        return RedirectToAction("Index");
    }

    var model = new myViewModel();
    model.Id = record.Id;
    // Pulling other records, nothing special

    return View(model);
}

[HttpPost]
public ActionResult Delete(int id, string returnUrl)
{
    ...
    var item = _repository.Get(id);
    if (item == null)
    {
        _myServices.Notifier.Error(T("Inquiry not found."));
    }
    else
    {
        _myServices.Notifier.Information(T("Request deleted successfully."));
        _repository.Delete(item);
    }

    return this.RedirectLocal(returnUrl, "~/");
}

DeleteDetails のような別のアクションを作成する必要があるかどうか疑問に思っていますが、Details アクションの record=null チェックは引き続き実行されます。

インデックス ビューと詳細ビューの両方の削除コードを次に示します。

@{using (Html.BeginForm("Delete", "MyAdmin", 
    new { area = "MyNameSpace" }, 
    FormMethod.Post, new { @class = "delete-form" }))
    {
        @Html.AntiForgeryTokenOrchard()
        @Html.Hidden("id", Model.Id)
        @Html.Hidden("returnUrl", Context.Request.ToUrlString())
        <input type="submit" value="Delete" />
    }
}

詳細ビューの削除コードを変更する必要がありますか?

何かご意見は?

4

1 に答える 1