ModelState.Isvalid 将 mvc 路由 ID 更改为字符串时抛出异常?
ModelState.Isvalid throwing exception when changing the mvc routing id as string?
我将路由配置文件 ID 更改为字符串
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Agent_default",
"Agent/{controller}/{action}/{id}",
new { Controller = "Home", action = "Index", id = "" }
);
}
现在我可以像这样得到我的 url http://localhost:43425/Agent/Accounts/Edit/A64h9lStdffgt
但是我在保存模型时遇到异常。
抛出异常ModelState.Isvalid - 错误
表示:值 'A64h9lStdffgt' 对 Id 无效。如何解决这一问题? ModelState.Isvalid 是如何运作的?为什么会出现此错误?
谢谢
代表@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 }
);
}
我将路由配置文件 ID 更改为字符串
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Agent_default",
"Agent/{controller}/{action}/{id}",
new { Controller = "Home", action = "Index", id = "" }
);
}
现在我可以像这样得到我的 url http://localhost:43425/Agent/Accounts/Edit/A64h9lStdffgt
但是我在保存模型时遇到异常。
抛出异常ModelState.Isvalid - 错误
表示:值 'A64h9lStdffgt' 对 Id 无效。如何解决这一问题? ModelState.Isvalid 是如何运作的?为什么会出现此错误?
谢谢
代表@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 }
);
}