0

客户端验证不起作用...... 我在另一个页面中完成了相同的过程,但没有使用 forloop。在这里,当我在不进行验证的情况下键入文本时,只需勾选图像正在显示...给我解决方案..

视图是

 @model IList<clientval.Models.ShoppingClass>
 @using (Html.BeginForm("Login", "Home"))
        {
            for (int i =0; i <1; i++)
            {


        <table>
        <tr>
        <td>@Html.Label("FirstName")</td>
        <td>@Html.TextBox("FirstName")<div>@Html.ValidationMessageFor(o => o[i].FirstName)</div></td>
        <td>@Html.Label("LastName")</td>
        <td>@Html.TextBox("LastName")<div>@Html.ValidationMessageFor(o => o[i].LastName)</div></td>
        <tr>
        }
        }

脚本是

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="../../assets/js/ClientScript.js" type="text/javascript"></script>

控制器是

[AcceptVerbs(HttpVerbs.Post)]
 public ActionResult Guestlogin(string firstname, string lastname)
        {

              ShoppingClass s = new ShoppingClass();
              var button = Request["button"];
              var ob = s.Newcustomer(customerfirstname, customerlastname);
             TempData["BN"] = ob;
            return RedirectToAction("Sucessfully", ob);

        }

型号是

    [Required(ErrorMessage = "First Name is Required")]
    [RegularExpression(@"^[a-zA-Z ]*$", ErrorMessage = "First Name is Not valid")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is Required")]
    [RegularExpression(@"^[a-zA-Z ]*$", ErrorMessage = "Last Name is Not valid")]
    public string LastName { get; set; }
    public List<ShoppingClass> Newcustomer(string firstname, string lastname)
        {

            List<ShoppingClass> list = new List<ShoppingClass>();
            ShoppingClass obj = new ShoppingClass();
.
.
.
            }
4

2 回答 2

1

不太确定你在用for-loop 做什么,但我猜这只是一个例子?

你不应该像这样使用 TextBoxFor 和 LabelFor :

@using (Html.BeginForm("Login", "Home"))
        {
            for (int i =0; i <1; i++)
            {


        <table>
        <tr>
        <td>@Html.LabelFor(o => o[i].FirstName)</td>
        <td>@Html.TextBoxFor(o => o[i].FirstName)<div>@Html.ValidationMessageFor(o => o[i].FirstName)</div></td>
        <td>@Html.LabelFor(o => o[i].LastName)</td>
        <td>@Html.TextBoxFor(o => o[i].LastName)<div>@Html.ValidationMessageFor(o => o[i].LastName)</div></td>
        <tr>
        }
        }
于 2012-07-27T09:06:18.143 回答
0

尝试这个:

Html.Validate("FirstName");

在所有领域都这样

于 2012-07-27T11:07:02.803 回答