验证相应操作上的特定表单控件
本文关键字:表单 控件 操作上 验证 | 更新日期: 2023-09-27 18:33:32
在我的MVC Web应用程序中,我有两个Html.BeginForm()
具有相同的控制器和对单个视图的不同操作。当我单击第一个Html.BeginForm()
的按钮时,它会显示两个表单的验证消息,而我只希望它仅适用于单个表单。看看下面的代码。
//First Html.BeginForm()
@using (Html.BeginForm("AddFirst", "Student", FormMethod.Post, new { id = "form1", role = "form" }))
{
<tr>
<td>
@Html.LabelFor(model => model.MobileNo)
@Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { id = "TextMobile", @class = "form-control", @maxlength = "10", @placeholder = "Mobile No" } })
@Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
</td>
<td>
<input type="submit" name="submit" value="First Button" class="btn btn-default" />
</td>
</tr>
}
//Second Html.BeginForm()
@using (Html.BeginForm("AddSecond", "Student", FormMethod.Post, new { id = "form2", role = "form" }))
{
<tr>
<td>
@Html.LabelFor(model => model.MobileNo)
@Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { id = "TextMobile2", @class = "form-control", @maxlength = "10", @placeholder = "Mobile No" } })
@Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
</td>
<td>
<input type="submit" name="submit" value="Second Button" class="btn btn-default" />
</td>
</tr>
}
在上面的情况下,当我单击第一个按钮时,它会显示两个手机号码的验证消息,但我想显示一个手机号码的验证消息(第二个按钮相同(。如何仅验证特定形式的单个文本框?为此,我采取了不同的行动。在我的控制器中,我有两个操作。
[HttpPost]
public ActionResult AddFirst(string submit, User u)
{
agree = new Agree(u);
if (ModelState.IsValid)
{
//Some logic
return View("ThisView", agree);
}
else
{
return View("ThisView", agree);
}
}
[HttpPost]
public ActionResult AddSecond(string submit, User u)
{
agree = new Agree(u);
if (ModelState.IsValid)
{
//Some logic
return View("ThisView", agree);
}
else
{
return View("ThisView", agree);
}
}
在我的模型中,我有
[DisplayName("Mobile Number")]
[Required(ErrorMessage = "* Please Enter Mobile Number")]
public string MobileNo { get; set; }
您可以通过创建三个模型类来执行此操作
public class AddFirstMobileViewModel
{
[DisplayName("Mobile Number")]
[Required(ErrorMessage = "* Please Enter Mobile Number")]
public string MobileNo { get; set; }
}
public class AddSecondMobileViewModel
{
[DisplayName("Mobile Number")]
[Required(ErrorMessage = "* Please Enter Mobile Number")]
public string MobileNo { get; set; }
}
public class MobileViewModel
{
public AddFirstMobile FirstMobileNumber{get;set;}
public AddSecondMobile SecondMobileNumber{get;set;}
}
在视图中更新代码,例如
@model AppName.Models.MobileViewModel
@using (Html.BeginForm("AddFirst", "Student", FormMethod.Post, new { id = "form1", role = "form" }))
{
<tr>
<td>
@Html.LabelFor(model => model.FirstMobileNumber.MobileNo)
@Html.EditorFor(model => model.FirstMobileNumber.MobileNo, new { htmlAttributes = new { id = "TextMobile", @class = "form-control", @maxlength = "10", @placeholder = "Mobile No" } })
@Html.ValidationMessageFor(model => model.FirstMobileNumber.MobileNo, "", new { @class = "text-danger" })
</td>
<td>
<input type="submit" name="submit" value="First Button" class="btn btn-default" />
</td>
</tr>
}
//Second Html.BeginForm()
@using (Html.BeginForm("AddSecond", "Student", FormMethod.Post, new { id = "form2", role = "form" }))
{
<tr>
<td>
@Html.LabelFor(model => model.SecondMobileNumber.MobileNo)
@Html.EditorFor(model => model.SecondMobileNumber.MobileNo, new { htmlAttributes = new { id = "TextMobile2", @class = "form-control", @maxlength = "10", @placeholder = "Mobile No" } })
@Html.ValidationMessageFor(model => model.SecondMobileNumber.MobileNo, "", new { @class = "text-danger" })
</td>
<td>
<input type="submit" name="submit" value="Second Button" class="btn btn-default" />
</td>
</tr>
}
在此之后,单击一个表单的"提交"按钮显示对相应表单的验证并将数据保存到数据库
希望这有帮助。