ACP*_*ACP 205 forms asp.net-mvc jquery
我想使用jquery验证我的表单,但它现在没有ID属性如何将其添加到asp.net mvc中的表单?我正在使用这个......
<% using (Html.BeginForm()) {%>
Run Code Online (Sandbox Code Playgroud)
我的jquery验证器插件接受了这个,
var validator = $("#signupform").validate({
Run Code Online (Sandbox Code Playgroud)
现在我想把id作为signupform...任何建议......
Jas*_*owe 339
这应该添加id.
ASP.NET MVC 5及更低版本:
<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "signupform" }))
{ } %>
Run Code Online (Sandbox Code Playgroud)
ASP.NET Core:您可以在表单中使用标记帮助程序,以避免设置id的奇怪语法.
<form asp-controller="Account" asp-action="Register" method="post" id="signupform" role="form"></form>
Run Code Online (Sandbox Code Playgroud)
我已经为我的项目添加了一些代码,所以它更方便.
HtmlExtensions.cs:
namespace System.Web.Mvc.Html
{
public static class HtmlExtensions
{
public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId)
{
return htmlHelper.BeginForm(null, null, FormMethod.Post, new { id = formId });
}
public static MvcForm BeginForm(this HtmlHelper htmlHelper, string formId, FormMethod method)
{
return htmlHelper.BeginForm(null, null, method, new { id = formId });
}
}
}
Run Code Online (Sandbox Code Playgroud)
MySignupForm.cshtml:
@using (Html.BeginForm("signupform"))
{
@* Some fields *@
}
Run Code Online (Sandbox Code Playgroud)
在System.Web.Mvc.Html (在System.Web.Mvc.dll中)中,开始表单定义如下: - 详细信息
BeginForm(这个HtmlHelper htmlHelper,字符串actionName,字符串
controllerName,对象routeValues,FormMethod方法,对象htmlAttributes)
意味着你应该这样使用:
Html.BeginForm(string actionName,string controllerName,object routeValues,FormMethod方法,object htmlAttributes)
所以,它在MVC 4中有效
@using (Html.BeginForm(null, null, new { @id = string.Empty }, FormMethod.Post,
new { @id = "signupform" }))
{
<input id="TRAINER_LIST" name="TRAINER_LIST" type="hidden" value="">
<input type="submit" value="Create" id="btnSubmit" />
}
Run Code Online (Sandbox Code Playgroud)
可能有点晚了,但就我而言,我不得不将 id 放在第二个匿名对象中。这是因为第一个用于路由值,即返回 URL。
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { id = "signupform", role = "form" }))
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助某人:)
| 归档时间: |
|
| 查看次数: |
171249 次 |
| 最近记录: |