html - ASP.NET MVC - Razor - 使用操作、 Controller 和 html 属性重载按钮

标签 html asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4

我想使用按钮而不是 ActionLink 重写下面的代码。任何帮助将不胜感激。谢谢。

@Html.ActionLink("Test Link","Index", "Home", new { id = item.id})

是否可以像下面这样写,或者有更好的方法吗?我也不确定语法,所以请随时更正它。

<input type="button" value="Submit" onclick="location.href='@Url.Action("Index", "Home", new { id = @Model.FirstorDefault.Id.ToString()}, null)'" />

最佳答案

改为将其写为 anchor 标记,因为您似乎想在单击按钮时重定向用户。如果您愿意,您可以轻松地将锚定样式设置为看起来像按钮。

    <a href="@Url.Action("Index", "Home", 
        new { id = Model.FirstorDefault.Id.ToString()})">Submit</a>

现在“提交”标题没有意义,因为您正在执行“GET”操作而不是“POST”。但如果你坚持使用按钮,你可以这样做:

<input type="button" value="Submit" id="btn1" />

// just replace zero with whatever value you need to use
$("#btn1").click(function () {
    window.location.replace('@Url.Action("about", "Home", new { id = 0 })');
});

关于html - ASP.NET MVC - Razor - 使用操作、 Controller 和 html 属性重载按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15945469/

相关文章:

css - 强制div填充指定区域?

php - 动态保存所见即所得内容的概念

c# - 异常时通知用户

javascript - MVC-Mini-Profiler 错误 : yepnope is not defined

asp.net - 带有 MVC 3 的 MySql 连接器/网络

html - 如何设置一个div的宽度等于它的sibling img的宽度

javascript - 如何将 JSON 数据添加到 nvd3 图表

c# - db.SaveChanges() 导致 System.Data.Entity.Infrastructure.DbUpdateException' 发生在 EntityFramework.dll 中

c# - Entity Framework DataContext 问题 - 它是否在我的 Controller 中得到正确处理?

c# - Microsoft JScript 运行时错误 : 'Sys' is undefined (in mvc3 c# razor)