我大部分时间都在追逐我的尾巴。那里有许多很棒的建议,但没有一个可以解决我的所有问题:
我们有一个现有的网站,其中有人将链接按钮隐藏到绝对地址,我正试图找到一种方法来做到这一点。我已经掌握了大部分内容,但是有一个带有样式按钮和编队的线条才能胜任。原始代码是这样的:
@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保留为按钮图像,并在单击按钮时保留形成。
如果我将按钮更改为<input>
,则会丢失<span>
,因此会丢失字形。
如果我在按钮中拨打@Url.Action()
,请执行以下操作:
<button type="submit" onclick="location.href='@Url.Action("Action","Model")'" class="btn.... etc.
它似乎没有在帖子中提交任何内容。如果我在BeginForm()中指定Action和Model,我会找到一个未找到的资源,因为它试图找到View而不只是动作...
那么, 是什么更改此按钮的绝对链接到ASP.NET MVC的正确方法,这将激活控制器和中的操作可以设置为带有glyphicon作为图像的按钮吗?
答案 0 :(得分:1)
问题在于提交按钮上的results
属性。此属性用于指向与formaction="Model/Action"
方法中定义的Controller和Action不同的Controller和Action。如果您在同一表单中有多个提交按钮,这应该提交给不同的操作,这非常有用。
在您的情况下,Html.BeginForm()
方法中没有定义Controller和Action,因此Html.BeginForm()
属性应指向Controller操作,该操作接受与模型类型匹配的参数。现在,它指向formaction
。您收到"Model/Action"
错误,因为您要么没有名为HTTP 404
的控制器,要么控制器没有名为ModelController
的操作。更改Action
属性的控制器或操作部分以指向正确的控制器或操作。
答案 1 :(得分:1)
所以,非常感谢@LarsKristensen帮助我解决这个问题:仅仅是为了我自己的参考和其他任何陷入困境的人,这里是我的转换:
自:
<button type="submit"
id="Save"
value="Save"
formaction="/somecontroller/someaction"
class="btn btn-primary">
<span class="glyphicon glyphicon-floppy-save">
</span>
</button>
有绝对链接,我现在有:
@using{Html.BeginForm("someaction","somecontroller", FormMethod.Post))
{
// ... lots of other HTML and form elements
<button type="submit"
id= "Save"
value = "Save"
onclick="location.href='@Url.Action("someaction","somecontroller","Save")'"
class="btn btn-primary">
<span class="glyphicon glyphicon-floppy-save">
</span>
</button>
// .. lots of other HTML and form elements
}
有一点需要注意。这只适用于一个提交按钮。