c# - 错误操作编辑 Controller MVC

标签 c# asp.net .net asp.net-mvc asp.net-mvc-4

*

I have this error with my action. Error My Controller

    [Authorize(Roles = ApplicationRoles.PAINELCHAMADA_EDITAR)]
    public ViewResultBase Editar(int IdEmpresa, int id)
    {
      var model = this.Service.Get(IdEmpresa, id);
      return base.PartialView(model);
    }

    [HttpPost]
    [Authorize(Roles = ApplicationRoles.PAINELCHAMADA_EDITAR)]
    public ActionResult Editar(PainelChamada item)
    {
      if (this.ModelState.IsValid)
      {
        try
        {
          this.Service.Update(item, this.GetFieldsToUpdate());
          return RedirectToAction("Index");
        }
        catch (ValidationException exception)
        {
          base.AddValidationErrors(exception);
          return base.View(item);
        }
      }
      else
      {
        return base.View(item);
      }
    }

View @model PainelChamada @{ this.ViewBag.Title = PainelChamadaResource.Titulo; this.ViewBag.SubTitle = Geral.Editar;
} @using (Html.BeginForm( this.DefaultActionEdit, "PainelChamada", "POST")) //new AjaxOptions //{ // InsertionMode = InsertionMode.Replace, // HttpMethod = "POST", // UpdateTargetId = "content" //})) { @Html.ValidationSummary() @Html.HiddenFor(i => i.Id)

    <input type="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" value="Alterar" />
} what might be happening? I try create routes in GlobalAsax, but no have success.

*

最佳答案

这意味着有人向 Editar 操作发送请求并且没有为 IdEmpresa 参数指定值。

如果你想避免这个异常,你可以在路由映射中将 IdEmpresaid 参数标记为可选,并在你的方法中使它们可以为空:

  public ViewResultBase Editar(int? IdEmpresa, int? id)

但在这种情况下,您必须在操作中使这些值无效。

这种行为在网络爬虫中很常见。有一次,我遇到了同样的问题,所以我记录了所有发出这些请求的 IP——所有这些都是由谷歌网络爬虫完成的。所以就我而言,我只是忽略了那些异常(exception)

关于c# - 错误操作编辑 Controller MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506098/

相关文章:

c# - 如何在 Windows 中打开/关闭 radio ?

c# - EF AddOrUpdate 种子不更新子实体

c# - 如何中止 Crystal 报表

javascript - 如何以相同的格式显示div内容?内容取自数据库

c# - MVVM 绑定(bind)、对象属性通知已更改

c# - 简单的闪烁文本

c# - 在asp.net中同时执行代码行

.net - ASP.NET MVC 3.0.0.1 版安全补丁破坏了构建

c# - 为什么允许垃圾收集器使用终结器收集看似引用的对象?

c# - 有什么方法可以让代码契约与 LINQ 一起工作吗?