1

I am creating a register login page in asp mvc and as i need, this page has two models and two form actions. every thing is ok but the validation. the models are:

public class Account_Index_ViewModel
{
    public UserAccount_Login_ViewModel userAccount_Login_ViewModel { get; set; }
    public UserAccount_Register_ViewModel userAccount_Register_ViewModel { get; set; }
}

public class UserAccount_Login_ViewModel
{
    [Required]
    [DataType(DataType.Password)]
    public string Pass { get; set; }
    [Required]
    public string LoginName { get; set; } // NickName/Email/MobilePhone
}

public class UserAccount_Register_ViewModel
{
    public string NickName { get; set; }
    public string Passw { get; set; }
    public string PassConfirm { get; set; }
    public string Email { get; set; }
    public string MobilePhone { get; set; }
}

and the view :

@model GhafasehWebSite.Models.Account_Index_ViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div class="AccountBook">
    <div class="half-width">
        @using (Html.BeginForm("Login", "Account"))
        {
            Html.EnableClientValidation();
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>ورود به سیستم</h4>
                <hr />
                @Html.ValidationSummary(true)

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName, new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName, new { @class = "form-control", placeholder = "نام مستعار/ایمیل/شماره موبایل" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass, new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass)
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-4 col-md-10">
                        <input type="submit" value="ورود" class="btn btn-primary" />
                    </div>
                </div>
            </div>
        }
    </div>
    <div class="half-width">
        @using (Html.BeginForm("Register","Account"))
        {
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>ثبت نام در سیستم</h4>
                <hr />
                @Html.ValidationSummary(true)

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone)
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-4 col-md-10">
                        <input type="submit" value="ثبت نام" class="btn btn-success" />
                    </div>
                </div>
            </div>
        }
    </div>
</div>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

and the controller:

    [HttpGet]
    public ActionResult Index()
    {
        return View(new Account_Index_ViewModel());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(UserAccount_Login_ViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (DataProvider.LoginUser(model, ModelState, Request, Session))
            {
                return RedirectToAction("Index", "Home");
            }
        }
        return View("Index");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(UserAccount_Register_ViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (DataProvider.RegisterUser(model, ModelState, Request, Session))
            {
                return RedirectToAction("Index", "Home");
            }
        }
        return View("Index");
    }

you should know that server side validation works fine but the client side is asleep. So, what do you suggest?

ConductedClever
  • 4,175
  • 2
  • 35
  • 69
  • 1
    Any javascript errors in the console? Have you verified your bundles are loading properly? And your Register model has no DataAnnotations, so there would be no validation. – Brandon Aug 19 '15 at 17:21
  • @Brandon, you where right. my bundles was not rendering correctly! THANKS. – ConductedClever Aug 19 '15 at 18:51

1 Answers1

2

When you say the validation doesn't work what do you mean? you press Login button with empty username/password and it doesn't show required error? If so , I Created new project with your model view controller and it worked !!! The validation works.

Elyana
  • 21
  • 2
  • thanks @Elyana. as i said in the comment, whole the story was because of bundle not being loaded correctly that i fixed that. Thnaks again. – ConductedClever Aug 19 '15 at 19:48