错误消息显示所有字段的相同错误

时间:2013-10-14 21:35:46

标签: c# asp.net-mvc-4

我的注册表视图中的所有字段都收到相同的模型错误消息。这是我的Register Post方法的代码。

    [CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")]
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterRequest model)
    {
        if (ModelState.IsValid)
        {

            if (Membership_BAL.UsernameExist(model.UserName.Username))
            {
                ModelState.AddModelError("CustomError", "Usename is taken");

                return View();
            }

            if (Membership_BAL.EmailExist(model.EmailAddress.EmailAddress))
            {
                ModelState.AddModelError("CustomError", "Email Address is taken");
                return View();
            }

            if (
                !Membership_BAL.CamparaEmailAddress(model.EmailAddress.EmailAddress,
                    model.EmailAddress.ComfirmEmailAddress))
            {
                ModelState.AddModelError("CustomError", "Email Address must match");
                return View();
            }


            Membership_BAL.Register(model);
            // TODO: Redirect use to profile page
            return RedirectToAction("Index", "Home");
        }

        TempData["Message"] = "Error: captcha is not valid.";
        return View();
    }

这是视图

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<fieldset>
    <legend> Register Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => Model.UserName.Username)
            @Html.EditorFor(m => Model.UserName.Username)
            @Html.ValidationSummary()
        </li>
        <li>
            @Html.LabelFor(m => Model.FirstName)
            @Html.EditorFor(m => Model.FirstName)
        </li>
        <li>
            @Html.LabelFor(m => Model.LastName)
            @Html.EditorFor(m => Model.LastName)
        </li>
        <li>
            @Html.LabelFor(m => Model.EmailAddress.EmailAddress)
            @Html.EditorFor(m => Model.EmailAddress.EmailAddress)
            @Html.ValidationSummary()
        </li>
        <li>
            @Html.LabelFor(m => Model.EmailAddress.ComfirmEmailAddress)
            @Html.EditorFor(m => Model.EmailAddress.ComfirmEmailAddress)
            @Html.ValidationSummary()
        </li>
        <li>
            @Html.MathCaptcha()
           @TempData["Message"]
        </li>
    </ol>

    <input type="submit" value="Register">

</fieldset>
}

2 个答案:

答案 0 :(得分:4)

没有看到视图或模型,它很安静,但我很确定它与此行有关的问题

ModelState.AddModelError("CustomError", "Email Address must match");

此方法的第一个参数接收字段名称,您将在其中显示错误,我在您的操作中看到所有错误在字段中具有相同的名称。

你应该改变这个:

ModelState.AddModelError("CustomError", "Email Address must match");

有关:

//Assuming that the field it's called Email    
ModelState.AddModelError("Email", "Email Address must match"); 

等等模型中的每个元素

答案 1 :(得分:1)

试试这个

         if (ModelState.IsValid)
        {
         bool valid=true;
        if (Membership_BAL.UsernameExist(model.UserName.Username))
        {
            ModelState.AddModelError("CustomError", "Usename is taken");

            valid=false;
        }

        if (Membership_BAL.EmailExist(model.EmailAddress.EmailAddress))
        {
            ModelState.AddModelError("CustomError", "Email Address is taken");
             valid=false;
        }

        if (
            !Membership_BAL.CamparaEmailAddress(model.EmailAddress.EmailAddress,
                model.EmailAddress.ComfirmEmailAddress))
        {
            ModelState.AddModelError("CustomError", "Email Address must match");
             valid=false;
        }
       if (!valid)
       {
          return view();
        }

        Membership_BAL.Register(model);
        // TODO: Redirect use to profile page
        return RedirectToAction("Index", "Home");
    }