【问题标题】:How to recognize the view.cshtml that you are coming from using ASP.NET Core MVC?如何识别您来自使用 ASP.NET Core MVC 的 view.cshtml?
【发布时间】:2020-12-10 00:05:46
【问题描述】:

我有 2 个视图:

lista.cshtml

<a href="@Url.Action("Eliminar","Productos", new {id = @item.Id})" class="btn btn-danger">Eliminar</a>

cuadricula.cshtml

<a href="@Url.Action("Eliminar","Productos", new {id = @Model.Producto.Id})"  class="btn btn-danger" hidden="@Model.Modificar" >
    Eliminar Producto
</a>

它们具有相同的操作,但我想在控制器中区分它们,因为我想返回不同的return RedirectToAction();

艾利米纳

public IActionResult Eliminar(int? id)
{
        // .. rest of the code

        if(you are coming from lista.chtml)
        {
           return RedirectToAction("return_1");
        }
        else if(you are coming from cradicula.cshtml)
        {
           return RedirectToAction("return_2");
        }
        //.. so on
}

我怎样才能做到这一点?唯一的解决方案是为每个不同的视图创建另一个 Eliminar 控制器,但我会违反 DRY。

【问题讨论】:

    标签: c# html asp.net-core asp.net-core-mvc


    【解决方案1】:

    只需在 Eliminar 方法中添加第二个参数,您就可以用一个值来填充该参数,该值指示重定向应该到达的位置。

    例如

    public IActionResult Eliminar(int? id, string redirectTo)
    

    <a href="@Url.Action("Eliminar","Productos", new {id = @item.Id, redirectTo = "lista"})" class="btn btn-danger">Eliminar</a>
    

    希望其余的都很明显。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多