使用Html.BeginForm张贴到电流控制器 [英] Using Html.BeginForm to post to the current controller
问题描述
我有一个在多个视图中使用的部分。
部分包含窗体。当窗体submited的动作是百达相同,但多数民众赞成控制器包含了动作取决于视图。
I have a partial that is used in several views. The partial contains forms. The action when a form is submited is allways the same, but the controller thats contains the action depends on the view.
可以说我有控制器,每有一个动作ActOnChoosenPerson:
Lets say I have Controllers that each have an action ActOnChoosenPerson:
FireStaffController
FireStaffController
HireStaffController
HireStaffController
我有一个部分
PersonList.ascx
PersonList.ascx
的形式呈现为
Html.BeginForm(FireStaffController,
ActOnChoosenPerson)或
Html.BeginForm("FireStaffController" , "ActOnChoosenPerson") or
Html.BeginForm(HireStaffController,
ActOnChoosenPerson)
Html.BeginForm("HireStaffController" , "ActOnChoosenPerson")
什么是摆脱控制器参数的好方法吗?现在我通过电流控制器的名称到模型,并使用
What is a good way to get rid of the controller parameter? Right now I pass the name of the current controller into the model and use
Html.BeginForm(Model.CurrentController,
ActOnChoosenPerson)
Html.BeginForm(Model.CurrentController , "ActOnChoosenPerson")
但是这是一个有点尴尬。
but that is a bit awkward.
推荐答案
这code将永远给你的电流控制器
This code will always give your current controller
<%=( Url.RequestContext.RouteData.GetRequiredString("Controller")) %>
显然,你可以使用它没有在&lt;%=像这样
Obviously you can use it without the <%= like this
Html.BeginForm(
Url.RequestContext.RouteData.GetRequiredString("Controller")) ,
"ActOnChoosenPerson")
看起来更笨重,但你的模型不应该需要知道什么控制器调用它。
It looks more clunky but your model should not need to know what controller is calling it.
这篇关于使用Html.BeginForm张贴到电流控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!