将mvc路由ID更改为字符串时,ModelState.Isvalid抛出异常?

时间:2015-03-19 11:29:07

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller asp.net-mvc-routing

我将路由配置文件ID更改为字符串

   public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Agent_default",
            "Agent/{controller}/{action}/{id}",
            new { Controller = "Home", action = "Index", id = "" }

        );
    }

现在我可以像我预期的那样得到我的网址http://localhost:43425/Agent/Accounts/Edit/A64h9lStdffgt

但是我在保存模型时遇到异常。

抛出异常ModelState.Isvalid - False

说:价值' A64h9lStdffgt'对于Id无效。怎么解决这个? ModelState.Isvalid如何工作?为什么我收到此错误?

由于

1 个答案:

答案 0 :(得分:2)

代表@Stephen Muecke:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Agent_Custom",
        "Agent/{controller}/{action}/{number}",
        new { Controller = "Home", action = "Index", number = "" }
    );

    context.MapRoute(
        "Agent_default",
        "Agent/{controller}/{action}/{id}",
        new { Controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}