我在表单上有 4 个按钮,“提交 1”调用控制器 A,“提交 2”调用控制器 B。
我在同一个表单上也有“按钮 1”和“按钮 2”,我不希望验证发生/触发这些。
如何使“提交 1”和“提交 2”验证表单而不是“按钮 1”和“按钮 2”?
同时'Button 1'和'Button 2'应该提交给不同的控制器。我如何实现这一目标?
public class SampleForm
{
[Display(Name = "FName: ")]
[Required(ErrorMessage = "Enter FName")]
public string FirstName { get; set; }
[Display(Name = "LName: ")]
[Required(ErrorMessage = "Enter LName")]
public string LastName { get; set; }
[Display(Name = "Bypassid: ")]
[Required(ErrorMessage = "Enter Bypassid")]
public string Bypassid { get; set; }
}
@model SampleForm
@using (Html.BeginForm("SampleForm", "SampleController", FormMethod.Post, new { name = "frmSample", id = "frmSample" }))
{
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
@Html.LabelFor(model => model.Bypassid)
@Html.TextBoxFor(model => model.Bypassid)
@Html.ValidationMessageFor(model => model.Bypassid)
<input type="submit" value="Submit 1" name="Command" />
<input type="submit" value="Submit 2" name="Command" />
<button type="button" name="Button 1" value="Button 1">Button 1</button>
<button type="button" name="Button 2" value="Button 2">Button 2</button>
}
我希望“提交 1”和“提交 2”使用 DataAnnotations 验证表单,但是当我单击“按钮 1”时,我想确保“Bypassid”字段中有值并重定向到另一个控制器(比如说。我单击“按钮 2”我不验证任何内容,只是重定向到另一个控制器。