使用T4MVC,如何在HTML表单上设置action属性(Using T4MVC, how to set the action attribute on an HTML form)
我正在采取我的第一个摇摇欲坠的步骤,使用jQuery提交Html表单。 一切运作良好,但想使用T4MVC生成动作链接。
这适用于Html.BeginForm(和Ajax.BeginForm),因为它们将ActionResult作为生成参数的动作。 即:
有办法吗?
我想我能做到:
@using (Html.BeginForm(MVC.MyArea.MyController.MyAction(),...,new {@id="myForm"}))
{
// Inputs
}
但真的很奇怪T4MVC是否可以解决这个问题。 怀疑不是,但我是新手,所以也许我错过了什么?
(是的,我知道Ajax.BeginForm,但我正在使用当前项目来了解有关MVC和jQuery的更多信息)。
I am taking my first teetering steps with submitting Html forms using jQuery. All works well, but would like to use the T4MVC to generate the action link.
This works with Html.BeginForm (and Ajax.BeginForm) because they take an ActionResult as the action generating param. Ie:
Is there a way to do:
I suppose I could do:
@using (Html.BeginForm(MVC.MyArea.MyController.MyAction(),...,new {@id="myForm"}))
{
// Inputs
}
But really wonder if T4MVC can handle this. Suspect not, but am new to it, so maybe am missing something?
(And yes, I know about Ajax.BeginForm, but am using the current project to learn more about MVC and jQuery).
原文:https://stackoverflow.com/questions/5084694
更新时间:2020-01-16 23:53
最满意答案
以下应该有效:
或者,如果您需要添加额外的路线值:
The following should work:
Or if you need to add extra route values:
相关问答
这个内容涵盖了这一点 。 基本上,尝试: MVC.Account.ExternalLoginConfirmation().Result.AddRouteValue(...)
This is covered in this thread. Basically, try: MVC.Account.ExternalLoginConfirmation().Result.AddRouteValue(...)
这是在VS补丁中修复的已知VS问题。 只需安装从https://msdn.microsoft.com/en-US/library/mt634751(VS.140).aspx ,它会照顾它。 另见https://github.com/T4MVC/T4MVC/issues/67 。 This is a known VS issue that was fixed in a VS patch. Simply install from https://msdn.microsoft.com/en-US/lib
...
不确定这是否是最好的方式,但这有效: filterContext.Result = new RedirectResult(Url.Action(MVC.Account.Login(ServiceLocator.ExceptionHelper().NotLoggedInError.Code)));
Not sure if it's the best way, but this works: filterContext.Result = new RedirectResult(Url.Action(M
...
对不起,之前的改变打破了它。 我刚刚推出了一个新版本的T4MVC(2.6.66)来解决这个问题。 好吧,它更像是一种解决方法,因为它在使用UseLowercaseRoutes时基本上不会生成常量标记。 但那会让我们现在开始。 问题的根源是C#不支持在常量字符串中使用.ToLowerInvariant()。 理想情况下,它只会在编译时评估它,但它不是那么聪明:) Ah sorry, the previous change broke it. I just pushed a new build of
...
类似的事情也出现在T4MVC论坛上( 此线程 )。 我想我会继续并在T4MVC中添加对它的支持。 其实,我只是想到一个有趣的方法来解决这个问题。 加入重载来传递额外参数的问题在于,您需要为所有其他采用ActionResult的T4MVC扩展方法添加类似的重载,这会导致混乱。 相反,我们可以使用流畅的方法,无需付出任何努力即可在任何地方使用。 这个想法是,你会写:
bar.Name,
MVC.Bar.Details(bar.ID)
...
您是否确定重新运行T4MVC以基于最新版本生成(右键单击.tt文件/运行自定义工具)? 如果这不是问题,我可能需要看看有问题的示例应用程序,看看发生了什么。 Did you make sure to re-run T4MVC to generate based on the latest (Right click .tt file / run custom tool)? If that's not the problem, I may need to look at a sample app th
...
尝试这个:
MVC.Entity.Index(vaultViewItem.VaultID),
new { target = "vaultIFrame" })%>
Try this:
...
以下应该有效:
或者,如果您需要添加额外的路线值:
The following should work: <
...
更新(2011年12月7日):此问题现已修复(在2.6.65中)。 请参见http://mvccontrib.codeplex.com/workitem/7177 。 T4MVC确实生成了许多常量。 例如 对于控制器名称: MVC.Home.Name 对于动作名称: MVC.Home.ActionNames.About 对于视图名称: MVC.Home.Views.About Update (12/7/2011): this issue is now fixed (in 2.6.65). See
...
使用T4MVC,这样的东西应该工作:
...请注意,这是假设ItemDetail操作在QuoteController 。 如果控制器名称不同,请将Quote更改为控制器名称。 您还可以使用@ Zaphod的评论更多信息,并执行以下操作: