public ActionResult SignUp([Bind(Include = "UserName,Password")] UserProfile userProfile)
        {
            using (var context = new CarServiceSystemWeb.EntityContext.CarServiceContext())
            {
                if (ModelState.IsValid)
                {
                    context.UserProfiles.Add(userProfile);
                    context.SaveChanges();
                    return(RedirectToAction("Login", "Home"));
                }
                else
                {
                    string p = "NoErrorFound";
                    //Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    if (ViewData.ModelState.Values.Count != 0)
                    {
                        p = null;
                    }
                    foreach (ModelState modelState in ViewData.ModelState.Values)
                    {
                        foreach (ModelError error in modelState.Errors)
                        {
                            p += error.ErrorMessage + "\n";
                        }
                    }
                    return(Json(new { Error = p }, JsonRequestBehavior.AllowGet));
                }
            }

            return(PartialView("SignUpPartialView", userProfile));
        }
示例#2
0
        public JsonResult GetModels(int id)
        {
            //var models ;
            var context = new CarServiceSystemWeb.EntityContext.CarServiceContext();

            List <Model> models = new List <Model>();

            foreach (var mod in context.Models.Where(i => i.BrandID == id))
            {
                models.Add(new Model
                {
                    Id   = mod.Id,
                    Name = mod.Name
                });
            }
            //  Messagebox("dd");
            var selectedList = new SelectList(models, "Id", "Name");

            return(Json(new SelectList(models, "Id", "Name")));
        }
示例#3
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            using (var context = new CarServiceSystemWeb.EntityContext.CarServiceContext())
            {
                if (PropName == "UserProfile")
                {
                    foreach (UserProfile usp in context.UserProfiles)
                    {
                        if (usp.UserName == value.ToString())
                        {
                            return(new ValidationResult(ErrorMessage = "This username is already in use!"));
                        }
                    }
                }

                if (PropName == "Car")
                {
                    Car us       = (Car)validationContext.ObjectInstance;
                    Car realUser = null;
                    if (us.Id != 0)
                    {
                        // throw new Exception(us.Id.ToString());
                        realUser = context.Cars.Where(i => i.Id == us.Id).First();
                        context.Entry <Car>(realUser).Reload();
                    }
                    foreach (var user in context.Cars)
                    {
                        PropertyInfo property = typeof(Car).GetProperty(validationContext.MemberName);
                        var          val      = property.GetValue(user, null);
                        if (us.Id != 0)
                        {
                            var realVall = property.GetValue(realUser, null);
                            if (realVall.ToString() == val.ToString())
                            {
                                // throw new Exception(val.ToString());
                                continue;
                            }
                        }
                        if (val.ToString() == value.ToString())
                        {
                            return(new ValidationResult(ErrorMessage = "This " + validationContext.MemberName + " is already in use!"));
                        }
                    }
                }
                else if (PropName == "User")
                {
                    User us       = (User)validationContext.ObjectInstance;
                    User realUser = null;
                    if (us.Id != 0)
                    {
                        // throw new Exception(us.Id.ToString());
                        realUser = context.Users.Where(i => i.Id == us.Id).First();
                        context.Entry <User>(realUser).Reload();
                    }
                    foreach (var user in context.Users)
                    {
                        PropertyInfo property = typeof(User).GetProperty(validationContext.MemberName);
                        var          val      = property.GetValue(user, null);
                        if (us.Id != 0)
                        {
                            var realVall = property.GetValue(realUser, null);
                            if (realVall.ToString() == val.ToString())
                            {
                                // throw new Exception(val.ToString());
                                continue;
                            }
                        }
                        if (val.ToString() == value.ToString())
                        {
                            return(new ValidationResult(ErrorMessage = "This " + validationContext.MemberName + " is already in use!"));
                        }
                    }
                }
            }


            return(ValidationResult.Success);
        }