返回视图在URL中丢失ID



我有一个允许修改记录的网站。记录的页面URL以格式/编辑/112。

进行

我在命令上有2个按钮(保存/删除),该命令使用多量命令将数据转发到正确的操作。当转发到保存操作时,我然后检查模型状态是否有效。我遇到的问题是,如果模型状态为false,我想用模型将其重定向回编辑视图,并保持ID仍然完整。我目前正在遇到的问题,即返回视图时丢失了URL ID。

这是我的代码

 [HttpPost]
    [MultipleButton(Name = "action", Argument = "Update")]
    public ActionResult Update(CustomerViewModel model)
    {
        TempData.Keep();
        model.CustomerTypes = TempData["dropdownlist"] as IEnumerable<CustomerTypes>;

        if (!ModelState.IsValid)
        {
            return View("Edit", model);
        }
        var status = _customerService.UpdateCustomer(model.ToServiceModel());
        if (status.Result) return View("Index");
        ModelState.AddModelError("Model", status.Message);
        return View("Edit", model);
    }

任何帮助...

您有一堆自定义代码,这些代码都隐藏在那里,因此很难说,但是通常,要为包含ID的路线,它需要两个部分:

  1. 您的操作需要能够接受ID:

    public ActionResult Update(int id, CustomerViewModel model)
    
  2. 生成路由的URL时必须通过ID(在Html.ActionLink等,或Url.Action等,等等:

    @Html.ActionLink("Update this!", "Update", "MyController", new { id = Model.Id }) 
    

最新更新