如何在ASP.NET MVC的HTML中创建按钮? [重复]

问题描述 投票:8回答:2

我正在为索引页面制作一个小表单。

@model MarketPlace.Models.Download
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Downloads", System.Web.Mvc.FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <td>@Html.DisplayNameFor(model => model.EMail)</td>
    <td>@Html.TextBoxFor(model => model.EMail, new { @class = "form-control round-input", @style = "width:200px" })</td>
    <td>@Html.DisplayNameFor(model => model.Reference)</td>
    <td>@Html.TextBoxFor(model => model.Reference, new {@class = "form-control round-input", @style = "width:200px"})</td>
    <td>@Html.DisplayNameFor(model => model.Products)</td>
    <td>@Html.Buttonorsomething
}

我以为会有一个`@Html.Button或其他东西,我在Google上搜索,但显然他们没有添加一个。

我的问题是,我该如何制作一个按钮?

编辑:请注意,我一直在使用ASP.NET MVC很短的时间,请原谅我这个愚蠢的问题!

c# html asp.net asp.net-mvc razor
2个回答
12
投票

只需包含纯HTML按钮就可以了

喜欢

<input type="button" value="Create" /> 

如果你想要一个HTML按钮,那么它将调用你的MVC控制器的动作方法。试试这个

<input type="button" value="Create" onclick="location.href='@Url.Action("Create", "User")'" />

注意:最佳做法是避免使用突出的Javascript,您可以将其作为单独的onclick方法。

更新:

如果你真的想要一个帮助器,那么你可以创建一个自定义辅助类类型MvcHtmlString

一个很好的例子,你可以找到here


5
投票

你可以在Razor中使用HTML:

<input type="submit" />
<input type="button" />

每个HTML元素都没有帮助器。

© www.soinside.com 2019 - 2024. All rights reserved.