本文介绍了在ASP.NET MVC中使用@ Url.Action来执行formaction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天大部分时间都在追逐我的尾巴.有很多很棒的建议,但是没有一个能解决我的所有问题:

I've spent most of the day chasing my tail on this. There are many wonderful suggestions out there, but none that address all my problems:

我们有一个现有的网站,有人将按钮href链接到绝对地址,我正在设法找到一种方法.我已经掌握了大部分功能,但是一行带有样式按钮和formaction的行将不起作用.原始代码是这样的:

We have an existing website, where someone's linked button href's to absolute addresses, and I am trying to figure out a way to do that. I've got most of it, but one line with a styled button and formaction just won't work. The original code is like this:

@using(Html.BeginForm(null,null,FormMethod.Post))
{
   // ... a <div>, then a stack of buttons, that I've converted to
   // <a href="@Url.Action(.... and those buttons work

   <button class="submit" id="Save" value="Save" formaction="Model/Action" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-save"></span></button>

    // ... a stack of more buttons and checkboxes, that are all working...
}...

我需要保留glyphicon作为按钮图像,以及单击按钮时的formaction.

I need to retain the glyphicon as the button image, and the formaction when the button is clicked.

如果我将按钮更改为< input> ,则会丢失&span> ,从而丢失字形图标.

If I change the button to an <input>, I lose the <span> and hence the glyphicon.

如果我在按钮中调用 @ Url.Action(),如下所示:

If I call an @Url.Action() in the button, like this:

<button type="submit" onclick="location.href='@Url.Action("Action","Model")'" class="btn.... etc.

该帖子似乎未提交任何内容.如果我在BeginForm()中指定动作和模型,则会得到未找到的资源,因为它试图查找视图,而不仅仅是动作...

It doesn't seem to submit anything in the post. If I specify the Action and Model in the BeginForm(), I get a resource not found, because it's trying to find the View, instead of just the action...

因此, 是将具有绝对链接的按钮更改为ASP.NET MVC的正确方法,它将触发控制器中的操作.像按钮一样带有样式图标?

So, what is the proper way to change this button with the absolute link into an ASP.NET MVC something, that will fire the action in the controller and is stylable as a button with a glyphicon as image?

推荐答案

问题是由于提交按钮上的 formaction ="Model/Action" 属性所致.此属性用于指向与 Html.BeginForm()方法中定义的控制器和操作不同的控制器和操作.如果您有多个具有相同表单的提交按钮,这些按钮应该提交给不同的操作,则该功能很有用.

The issue is because of the formaction="Model/Action" attribute on the submit button. This attribute is used to point to a different Controller and Action, than the one defined in the Html.BeginForm() method. This is useful if you have multiple submit buttons in the same form, that should submit to different actions.

在您的情况下,您的 Html.BeginForm()方法中未定义控制器和动作,因此 formaction 属性应指向接受一个与模型类型匹配的参数.现在,它指向"Model/Action" .您收到 HTTP 404 错误,因为您没有名为 ModelController 的控制器,或者该控制器没有名为 Action .更改 formaction 属性的控制器或动作部分,使其指向正确的控制器或动作.

In your case, there is no Controller and Action defined in your Html.BeginForm() method, so the formaction attribute should point to a Controller action that accepts an argument matching the type of your model. Right now, it points "Model/Action". You get an HTTP 404 error, because you either don't have a controller called ModelController, or that controller doesn't have an action called Action. Change the controller or action part of the formaction attribute to point to the correct controller or action.

这篇关于在ASP.NET MVC中使用@ Url.Action来执行formaction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:13