使用ASP.net MVC是否可以将表单POST到控制器操作,该操作包括不在表单中的参数,但是来自URL?
例如
GroupController中的Action方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int idOne, int idTwo, Model model)
{ ... }
Run Code Online (Sandbox Code Playgroud)
路线:
"{controller}/{action}/{idOne}/{idTwo}"
Run Code Online (Sandbox Code Playgroud)
发布网址:
/Employee/Show/1/42
Run Code Online (Sandbox Code Playgroud)
在此示例中,表单将发布到不同的控制器,模型具有正确的值,但其他参数的默认值为0.
我期望的行为是ModelBinder会看到我有两个匹配给定路由的参数,并以与GET操作相同的方式将当前值1和42分配给参数.
这种行为不受支持,或者我错过了什么?
编辑:要清楚,Show控制器视图上的Employee表单包含一个发布到不同控制器的表单.我们可以称之为Group.
表单操作URL如下所示
/Groups/Create/0/0
Run Code Online (Sandbox Code Playgroud)
表格声明如下
Html.BeginForm("Create", "Groups")
Run Code Online (Sandbox Code Playgroud)
尝试了许多不同的重载后,Html.BeginForm我发现仅当表单操作URL与浏览器地址栏中的当前URL匹配时才会映射参数.
因此,如果我导航到URL,/Groups/Create/1/42我将有一个新表格.如果我然后提交表单,则URL路由值将传递给POST操作.
我正在使用默认的RazorViewEngine和Area布局配置,但是当我导航到使用该区域内的视图的链接时。我收到一条错误消息,指出在以下位置找不到和搜索视图:
~/Views/Applications/Details.cshtml
~/Views/Applications/Details.vbhtml
~/Views/Shared/Details.cshtml
~/Views/Shared/Details.vbhtml
Run Code Online (Sandbox Code Playgroud)
我发现奇怪的是,看起来好像视图引擎没有尝试搜索区域位置。我需要进行哪些调整才能使视图引擎在其区域中搜索视图。
这是我用来定义区域的相关代码。
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteRegistrar.RegisterRoutesTo(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
ApplicationAreaRegistration.cs
private void RegisterRoutesTo(RouteCollection routes)
{
routes.MapRoute("Application_default", AreaName + "/{action}/{applicationId}",
new
{
controller = "Applications",
action = "Index",
applicationDomainId = UrlParameter.Optional
}, new { applicationId = @"\d+" });
}
Run Code Online (Sandbox Code Playgroud)
Index.cshtml
@Html.RouteLink(item.Name, "Application_default", new { applicationId = item.Id, action = "Details" })
Run Code Online (Sandbox Code Playgroud)
物理目录布局
Areas \
\Application
\Controllers
-ApplicationsController.cs
\Views
-Details.cshtml
-ApplicationAreaRegistration.cs
Run Code Online (Sandbox Code Playgroud)