【问题标题】:ASP net core MVC Call diferent actions under 1 formASP net core MVC 1种形式下调用不同动作
【发布时间】:2020-05-07 13:15:48
【问题描述】:

我的控制器中有这个:

[HttpPost]
        public IActionResult Save(int IdentifikaceZ, ReklamaceModel model)
        {
            _db.Add(model);
            _db.SaveChanges();
            return RedirectToAction("MyMainView");
        }
@using (Html.BeginForm("Save", "MyMainView", FormMethod.Post))
    {
        ....
        <input id="Insert" name="Insert" value="Insert" type="submit">
        <input id="Edit" name="Edit" value="Edit" type="submit">
    }

我需要在表单中保存/编辑信息,但我不知道如何告诉服务器决定要做什么。因此,当我单击按钮 A 或按钮 B 时,它会执行相同的操作。我需要它来做单独的事情,但使用相同的元素(表单内的元素)感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    按钮被命名,命名按钮的工作方式是只有被点击的按钮才会进入 POST。因此,您可以简单地检查表单数据中是否存在一个键或另一个键:

    if (Request.Form.ContainsKey("Insert"))
        // do insert
    if (Request.Form.ContainsKey("Edit"))
        // do edit
    

    【讨论】:

    • 感谢您的回答,它帮助了我。也感谢您花时间解决这个问题。
    【解决方案2】:

    首先,将属性string ActionType 添加到您的ReklamaceMode 模型中。

    然后将您的 HTML 更改为:

    @using (Html.BeginForm("Save", "MyMainView", FormMethod.Post))
    {
        ....
        <input id="Insert" name="ActionType" value="Insert" type="submit">
        <input id="Edit" name="ActionType" value="Edit" type="submit">
    }
    

    现在在您的 csharp 代码中:

    [HttpPost]
    public IActionResult Save(int IdentifikaceZ, ReklamaceModel model)
    {
        // check model.ActionType, it will be either Insert or Edit
    }
    

    【讨论】:

    • 我无法将字符串 Action 类型添加到我的模型中,因为它从 db 读取并且它不知道列 actiontype,所以它给我一个错误,即 Action 类型现在是有效的列,但感谢您的回答
    • 你不应该在控制器中使用你的数据库实体(模型)!创建一个 DTO 并在请求中使用它,然后在映射后执行您的逻辑。
    【解决方案3】:

    添加和更新操作也可以用一种形式和一种方法来处理。

    添加id 作为隐藏表单控件,对于现有记录,此字段将具有一个值,即相关记录ID。对于新记录,默认值为 0(假设 id 类型为 int)。

    @using (Html.BeginForm("Save", "MyMainView", FormMethod.Post))
    {
        ....
    
        @Html.HiddenFor(x => x.Id)
    
        <input type="submit" name="submit" value="Save" />
    }
    

    在后端检查id值,如果>0则触发更新,否则为新记录。

    [HttpPost]
    public IActionResult Save(int IdentifikaceZ, ReklamaceModel model)
    {
       if(model.Id > 0)
           _db.Update(model);
       else
           _db.Add(model);
    
        _db.SaveChanges();
    
        return RedirectToAction("MyMainView");
    }
    

    这是一个简化的实现,在实际项目中,您需要在添加/更新之前对模型进行更多控制。例如,建议使用输入模型,然后将值绑定到 db 模型...

    如果您仍需要在一个表单中使用多个提交按钮来针对不同的后端操作,请参阅Multiple Submit Buttons 以了解标准表单和 ajax 表单。

    【讨论】:

    • 非常感谢。这是聪明的做法。感谢您花时间解决这个问题。也感谢您的链接。它对我帮助很大。
    【解决方案4】:

    在这里你可以这样做:::

     <form method="post" asp-controller="Home">
    <a id="Insert" name="Insert" value="Insert" type="submit" asp-action="Delete" asp-route-DeleteID="@model.DeleteID"/>
     <a id="Edit" name="Edit" value="Edit" type="submit" asp-action="Edit" asp-route-DeleteID="@model.EditID"/>
    </form>
    
    [HttpPost] public IActionResult Delete(int id) {
    
    }
    
    [HttpPost] public IActionResult Edit(int id) {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-08
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      相关资源
      最近更新 更多