Validate If user create same user name account than get Error -MVC

Code


       [Route("CreateNewAccount")]
        [HttpGet]

        public ActionResult CreateAccount()
        {
            return View();
        }

        [Route("CreateNewAccount")]
        [HttpPost]

        public ActionResult CreateAccount(MyLogin log)
        {
            FormDbEntities db = new FormDbEntities();
            var userdata = db.tbl_Login.Where(x => x.Userid == log.Userid).Count();
            if(userdata>0)
            {
                ViewData["usererro"] = "This User name Alredy used";
            }
            else
            {
                tbl_Login tb = new tbl_Login()
                {
                    FirstName = log.FirstName,
                    Userid = log.Userid,
                    password = log.password,
                    Copassword = log.Copassword,
                };
                //FormDbEntities db = new FormDbEntities();
                db.tbl_Login.Add(tb);
                int a = db.SaveChanges();
                if (a == 1)
                {
                    Session["data"] = "Welcome in new account" + "  " + tb.FirstName.ToString();
                    return RedirectToAction("Login");

                }
                else
                {
                    ViewBag.alert = "<script>alert('Account Creation Fail')</script>";
                    ViewData["Greteuser"] = "Welcome in new account" + tb.FirstName.ToString();
                }
            }
         
            return View();
        }


csHTML


@model Test.Models.MyLogin

@{
    ViewBag.Title = "CreateAccount";
}

@ViewData["CreateAccFailer"];
<h2 style="color:white">Create New Account</h2>
<div class="container mainsign " id="sig">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Userid, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Userid, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Userid, "", new { @class = "text-danger" })
                    <span> @ViewData["usererro"]</span>
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Copassword, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Copassword, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Copassword, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Contactno, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Contactno, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Contactno, "", new { @class = "text-danger" })
                </div>
            </div>


            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>

    }
</div>

<link href="~/Content/CreatenewAccount.css" rel="stylesheet" />

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls