错误操作编辑控制器MVC

时间:2014-12-16 13:47:17

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

*

  

我的动作有这个错误。   的错误       我的控制器

    [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);
      }
    }
     

查看       @model PainelChamada       @ {           this.ViewBag.Title = PainelChamadaResource.Titulo;           this.ViewBag.SubTitle = Geral.Editar;
      }       @using(Html.BeginForm(           this.DefaultActionEdit,           " PainelChamada&#34 ;,           " POST&#34))           //新的AjaxOptions           // {           // InsertionMode = InsertionMode.Replace,           // HttpMethod =" POST",           // UpdateTargetId ="内容"           //}))       {           @ 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.

*

2 个答案:

答案 0 :(得分:1)

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

如果您想避免此异常,可以在路线图中将IdEmpresaid参数标记为可选参数,并使其在您的方法中可为空:

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

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

此行为在网络抓取工具中非常常见。有一次,我遇到了同样的问题,所以我记录了发出这些请求的所有IP - 所有这些都是由Google网络抓取工具完成的。所以在我的情况下,我只是忽略了那些例外

答案 1 :(得分:0)


很高兴看到可用的路由映射和实际的URL,但您可能没有为Editar操作方法的任何一个或两个参数传递任何值。由于它接受的两个参数都是不可为空的int(并且我假设你没有在相应的路由映射中将它们中的任何一个作为可选项),你会得到上述异常。

相关问题