Asp.net mvc 使用HtmlHelper.BeginForm匹配复杂路由
我有一个复杂的路线,我想与一个HtmlHelper.BeginForm方法相匹配。我已经阅读了很多关于使用路由值字典、对象初始值设定项和html属性的文章和答案。但是他们都没有达到我想做的 以下是我要匹配的路线:Asp.net mvc 使用HtmlHelper.BeginForm匹配复杂路由,asp.net-mvc,forms,asp.net-mvc-4,html-helper,Asp.net Mvc,Forms,Asp.net Mvc 4,Html Helper,我有一个复杂的路线,我想与一个HtmlHelper.BeginForm方法相匹配。我已经阅读了很多关于使用路由值字典、对象初始值设定项和html属性的文章和答案。但是他们都没有达到我想做的 以下是我要匹配的路线: // Attempt to consolidate all Profile controller actions into one route routes.MapRoute( "Profile", "{adminUserCode}/{controller}s/{cus
// Attempt to consolidate all Profile controller actions into one route
routes.MapRoute(
"Profile",
"{adminUserCode}/{controller}s/{customerId}/{action}/{profileId}",
new { adminUserCode = UrlParameter.Optional, controller = "Profile"},
new { adminUserCode = @"\d+", customerId = @"\d+", profileId = @"\d+" }
);
我希望与之匹配的控制器和操作的url示例如下:
实际电话号码在邮件正文中
下面是我最接近正确的语法:
@using (Html.BeginForm(
"UpdatePhoneNumber",
"Profile",
new {
customerId = Model.LeadProfile.CustomerId,
profileId = Model.CustomerLeadProfileId
}))
{
<!-- the form -->
}
我知道我可以在这里使用原始HTML,但似乎应该有办法让愚蠢的HtmlHelper匹配比最基本的路由更多的路由。如果你想在表单中使用复杂的路由,你需要使用
如果要在表单中使用复杂的路由,则需要使用
这确实奏效了。我有点惊讶,我看到的资源中没有一个提到
BeginRouteForm
方法。简单阅读一下MSDN,我找不到任何例子或散文来解释这种差异,所以也许并不令人惊讶。我注意到,如果我只做Html.BeginRouteForm(“Profile”)
,它会用当前值填充整个路由,但是如果我尝试只传递new{action=“UpdatePhoneNumber”}
,它会失败。不过,您可以在新对象中删除冗余的控制器
属性。谢谢这确实奏效了。我有点惊讶,我看到的资源中没有一个提到BeginRouteForm
方法。简单阅读一下MSDN,我找不到任何例子或散文来解释这种差异,所以也许并不令人惊讶。我注意到,如果我只做Html.BeginRouteForm(“Profile”)
,它会用当前值填充整个路由,但是如果我尝试只传递new{action=“UpdatePhoneNumber”}
,它会失败。不过,您可以在新对象中删除冗余的控制器
属性。谢谢
<form method="post"
action="/mvc/123/Profiles/UpdatePhoneNumber?customerId=78293&profileId=1604750">
@using (Html.BeginForm(new
{
controller = "Profile",
customerId = Model.LeadProfile.CustomerId,
action = "UpdatePhoneNumber",
profileId = Model.CustomerLeadProfileId
}))
@using (Html.BeginRouteForm("Profile", new
{
controller = "Profile",
customerId = Model.LeadProfile.CustomerId,
action = "UpdatePhoneNumber",
profileId = Model.CustomerLeadProfileId
}))