如何在MVC 4中向表单添加ID [英] How do I Add ID to the Form in MVC 4

查看:80
本文介绍了如何在MVC 4中向表单添加ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre lang =   C#> @使用(Html.BeginForm( new  {id =   companyinfo }))
{
@ Html.AntiForgeryToken()
@ Html.ValidationSummary( true
< fieldset>
// 部分代码.........
< ; pre lang = XML>& lt; fieldset& gt; < / pre >


}



< / pre>



这是我的表格和我的代码想要提供身份证。但是,当我提交此表单时,它会给出错误.....



< pre lang =   C#>& lt; pre lang =& quot; C#& quot;& gt; [HttpPost] 
[ValidateAntiForgeryToken]
public ActionResult Create(CompanyMaster companymaster)
{

if (ModelState.IsValid)
{
db.CompanyMaster.Add(companymaster);
db.SaveChanges();
return RedirectToAction(& amp; quot; Index& amp; quot;);
}

return 查看(companymaster);

}& lt; / pre& gt;





在ModelState,属性& quot;公司信息&安培; QUOT;在这里无效.........< / pre>

解决方案

 @using(Html.BeginForm(Index ,Home,FormMethod.Post,new {id =companyinfo}))
{

}


< blockquote> MVC 5及更低版本:

 @ using(Html.BeginForm(null,null,FormMethod.Post,new {id =companyinfo}))
{

}



MVC 6:你可以使用标签帮助器来避免设置id的奇怪语法。

 <  表格    asp-controller   = 帐户    asp-action   = 注册   方法  =  post    id   =  companyinfo   角色  = 表单 >  <   / form  >  


<pre lang="C#">@using (Html.BeginForm(new { id = "companyinfo"}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<fieldset>
//Some code.........
<pre lang="XML">&lt;fieldset&gt;</pre>

 
}


</pre>

this is my code for form and i want to give an ID. But when I submit this form it gives the error at.....

<pre lang="C#">&lt;pre lang=&quot;C#&quot;&gt;[HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(CompanyMaster companymaster)
      {

              if (ModelState.IsValid)
              {
                  db.CompanyMaster.Add(companymaster);
                  db.SaveChanges();
                  return RedirectToAction(&amp;quot;Index&amp;quot;);
              }

          return View(companymaster);

      }&lt;/pre&gt;



At ModelState that the attribute &quot;companyinfo&quot; is not valid here.........</pre>

解决方案

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "companyinfo"}))
{
    
}


MVC 5 and lower:

@using (Html.BeginForm(null, null, FormMethod.Post, new {id="companyinfo"}))
   { 

} 


MVC 6 : You can use tag helpers to avoid the odd syntax for setting the id.

<form asp-controller="Account" asp-action="Register" method="post" id="companyinfo" role="form"></form>


这篇关于如何在MVC 4中向表单添加ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆