B
B
Bisekenov2018-07-25 18:42:37
ASP.NET
Bisekenov, 2018-07-25 18:42:37

Why does not add a record to the database table?

create a registration form. everything worked until I added validators.
After adding the validation function, no entry is added to the database table, after clicking the submit nothing happens, the page is refreshed stupidly and there are no errors and messages. and if you do not fill in the required fields, then a message about the required field is displayed. what am I doing wrong?
registration model:

Model
public int Id { get; set; }
        [Required]
        [Display(Name = "Логин")]  
        public string Loginu { get; set; }
     [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Пароль")]
        public string Pass { get; set; }
        [System.ComponentModel.DataAnnotations.Compare("Pass",ErrorMessage ="Пароли не совпадают!")]
        [DataType(DataType.Password)]
        public string PassConf { get; set; }
        public int? Identifier { get; set; }
        public int? Privilege { get; set; }
        [Required]
        [Display(Name = "Фамилия")]
        public string Surname { get; set; }
        [Required]
        [Display(Name = "Имя")]
        public string Name { get; set; }
        public string Middlename { get; set; }
        [Required]
        [Display(Name = "Пол")]
        public string Sex { get; set; }
        [Required]
        [Display(Name = "Дата рождения")]
        public DateTime DateofBirth { get; set; }
        [Required]
        [Display(Name = "Электронная почта")]
        public string Email { get; set; }
        [Required]
        [Display(Name = "Телефон")]
        public string Telephone { get; set; }
        [Required]
        [Display(Name = "Область")]
        public string Region { get; set; }
        [Required]
        [Display(Name = "Населенный пункт")]
        public string City { get; set; }
        public string School { get; set; }
        public int? SchoolId { get; set; }

controller:
controller

[HttpPost]
public ActionResult registration(Registration reg)
{
int selectedIndex = 1;
SelectList states = new SelectList(db.Spravregions, "Regionname", "Regionname", selectedIndex);
ViewBag.region = states;
SelectList cities = new SelectList(db.Spravcities.Where(c => c.RegionId == selectedIndex), "Cityname", "Cityname");
ViewBag.Cities = cities;
SelectList schoollist = new SelectList(db.Schoollists.Where(c => c.CityId == selectedIndex), "Schoolname", "Schoolname");
ViewBag.School = schoollist;
SelectList pollist = new SelectList(db.Pollists, "Pol", "Pol");
ViewBag.pollist = pollist;
SelectList identlist = new SelectList(db.Identlists, "Ident", "Ident");
ViewBag.identlist = identlist;
var chklogin = db.Logins.Where(m => m.Loginu == Loginu);
if (chklogin.Count() != 0) //проверка инвид.логина. Пока так, потом переделаю на ajax
{
ViewBag.chklog = "Логин занят";
return View(reg);
}
if (ModelState.IsValid)
{
db.Loginreg.Add(reg);
db.SaveChanges();
return RedirectToAction("ss","aa");
}
return View(reg);
}

view

<div class="otdblok"> 
        <h1 style="font-weight:bold;text-align:center;font-size:140%;">Регистрация</h1>
        @using (Html.BeginForm("registration", "Authorization", FormMethod.Post))
        {
            @Html.AntiForgeryToken();
            @Html.HiddenFor(m => m.Id)
            <span class="otdbloklabel">Логин:</span>
            @Html.EditorFor(m=>m.Loginu)
            @Html.ValidationMessageFor(m=>m.Loginu)
            @ViewBag.chklog
            <hr />
            <span class="otdbloklabel">Пароль:</span>
            @Html.EditorFor(m=>m.Pass)
            @Html.ValidationMessageFor(m=>m.Pass)
            <hr />
            <span class="otdbloklabel">Повторите пароль:</span>
            @Html.EditorFor(m=>m.PassConf)
            @Html.ValidationMessageFor(m=>m.PassConf)
            <hr />
            <span class="otdbloklabel">Кто вы:</span>
            @Html.DropDownList("Ident", ViewBag.identlist as SelectList, new { name = "Ident" })
            <hr />
            <span class="otdbloklabel">Фамилия:</span>
            @Html.EditorFor(m => m.Surname)
            @Html.ValidationMessageFor(m => m.Surname)
            <hr />
            <span class="otdbloklabel">Имя:</span>
            @Html.EditorFor(m => m.Name)
            @Html.ValidationMessageFor(m => m.Name)
            <hr />
            <span class="otdbloklabel">Отчество:</span>
            @Html.EditorFor(m => m.Middlename)
            @Html.ValidationMessageFor(m => m.Middlename)
            <hr />
            <span class="otdbloklabel">Пол:</span>
            @Html.DropDownList("Pol", ViewBag.pollist as SelectList, new {name = "Pol" })
            <hr />
            <span class="otdbloklabel">Дата рождения:</span>
            @Html.TextBoxFor(m=>m.DateofBirth, new { id = "dataroj"})
            @Html.ValidationMessageFor(m => m.DateofBirth)
            <hr />
            <span class="otdbloklabel">Электронная почта:</span>
            @Html.EditorFor(m => m.Email)
            @Html.ValidationMessageFor(m => m.Email)
            <hr />
            <span class="otdbloklabel">Номер телефона:</span>
            @Html.EditorFor(m => m.Telephone)
            @Html.ValidationMessageFor(m => m.Telephone)
            <hr />
            <span class="otdbloklabel">Область:</span>
            @Html.DropDownList("Regionname", ViewBag.region as SelectList, new { id = "state", name= "Regionname" })
            <hr />
            <span class="otdbloklabel">Город:</span>
            @Html.DropDownList("Cityname", ViewBag.Cities as SelectList, new { id = "city", name= "Cityname" })

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question