When updating database I get a EntityValidationError - asp.net-mvc

I'm trying to get the logged in user to edit his profile. For now I decided to use a satic userID (since using the sessions usedID was also giving me errors). Now when I run the code it goes through all the lines but at the end it gives me this error "EntityValidationErrors".
I just want the code to update the current UserID's Bio, Sex, and preferred sex
[HttpPost]
public ActionResult FillProfile(tblUser user)
{
using (TrinityEntities db = new TrinityEntities())
{
var id = Session["userID"];
user.Id = 1018; //Convert.ToInt32(id);
SqlConnection conn = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\Ruben\Desktop\Periode 2\Persoonlijk\Trinity\Trinity\Trinity\App_Data\Trinity.mdf; Integrated Security = True; MultipleActiveResultSets = True; Connect Timeout = 30; Application Name = EntityFramework");
conn.Open();
string Query = "SELECT FirstName, LastName, Email, Password, Age FROM tblUser WHERE Id='" + user.Id + "'";
using (SqlCommand command = new SqlCommand(Query, conn))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
user.FirstName = reader[0] as string;
user.LastName = reader[1] as string;
user.Email = reader[2] as string;
user.Password = reader[3] as string;
string Sage = reader[4] as string;
user.Age = Convert.ToInt32(Sage);
break;
}
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
conn.Close();
return RedirectToAction("Index", "Home");
}
}
View:
#model Trinity.Models.tblUser
#{
ViewBag.Title = "Profile";
// prevent login by alterring adress
if (Session["userID"] == null)
{
Response.Redirect("~/Login/Index");
}
}
<h2>Let's fill you profile!</h2>
#using (Html.BeginForm("FillProfile", "Profile", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Sex, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.Sex,
new List<SelectListItem> {
new SelectListItem { Value = "Male" , Text = "Male" },
new SelectListItem { Value = "Female" , Text = "Female" },
},
new {#class = "control-label col-md-2" })
#Html.ValidationMessageFor(model => model.Sex, "", new { #class = "text-danger" })
</div>
</div>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Preffered, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.Preffered,
new List<SelectListItem> {
new SelectListItem { Value = "Female" , Text = "Female" },
new SelectListItem { Value = "Male" , Text = "Male" },
new SelectListItem { Value = "Both" , Text = "Both" }
},
new {#class = "control-label col-md-2" })
#Html.ValidationMessageFor(model => model.Preffered, "", new { #class = "text-danger" })
</div>
</div>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.BIO, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextAreaFor(model => model.BIO, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.BIO, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label class="label-success">#ViewBag.SuccessMessage</label>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<label class="label-warning">#ViewBag.AlreadyRegisteredMessage</label>
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
Model classes:
//-------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//-------------------------------------------------------------------------
namespace Trinity.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class tblUser
{
public int Id { get; set; }
[DisplayName("First Name")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(#"^[^\W\d_]+$", ErrorMessage = "Only letters allowed in your name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(#"^[^\W\d_]+$", ErrorMessage = "Only letters allowed in your name")]
public string LastName { get; set; }
public string BIO { get; set; }
[DisplayName("E-Mail")]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(#"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Incorrect E-mail Format")]
public string Email { get; set; }
[DataType(DataType.Password)]
// [Required(ErrorMessage = "This field is required")]
[RegularExpression(#"^.*(?=.{8,})(?=.*[\d])(?=.*[\W]).*$", ErrorMessage = "Password must be 8 characters, contain at least 1 digit and one special character")]
public string Password { get; set; }
[DisplayName("Confirm Password")]
[DataType(DataType.Password)]
// [Required(ErrorMessage = "This field is required")]
[Compare("Password", ErrorMessage ="Passwords are not the same")]
public string ConfirmPassword { get; set; }
public string Photo { get; set; }
[DisplayName("Age")]
// [Required(ErrorMessage = "This field is required")]
public int Age { get; set; }
public string Sex { get; set; }
public string Preferred { get; set; }
}
}

Related

Validation Message for Indexed fields for Loop in MVC 5 Razor

I'm new and currently using Razor MVC 5 and I just want to ask if how to render the Validation message that shows on the indexed EditorFor. Currently Added a Data Annotation [Required (ErrorMessage = "Email Address is required")] and [DataType(DataType.EmailAddress)] but it shows a validation like The Char field is required.
Heres my set of Codes.
#using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createform" }))
var cnt = 0;
{
<div class="form-horizontal">
#Html.ValidationSummary(true)
#if (Model.AttendeeCount > 0 && Model != null)
{
for (int i = 0; i < Model.AdultCount; i++)
{
cnt++;
<div id="div_#cnt">
<div>Adult #i</div>
<div class="form-group">
#Html.LabelFor(model => model.FirstName[cnt], new { #class = "control-label col-md-2" })
<div class="col-md-6">
#Html.EditorFor(model => model.FirstName[cnt], new { htmlAttributes = new { #class = "form-control", required = "required", title = "First Name is required" } })
#Html.ValidationMessageFor(model => model.FirstName[cnt], "", new { #class = "text-danger" })
</div>
</div>
....
<div class="form-group">
#Html.LabelFor(model => model.Email[cnt], htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-6">
#Html.EditorFor(model => model.Email[cnt], new { htmlAttributes = new { #class = "form-control", required = "required", title = "Email is required" } })
#Html.ValidationMessageFor(model => model.Email[cnt], "", new { #class = "text-danger" })
</div>
</div>
</div>
}
}
</div>
}
Tried over riding the validation by adding new { htmlAttributes = new { #class = "form-control", required = "required", title = "Email is required" } })
but to no avail still shows the same vaidation error The Char field is required. Hope someone can help me with this
Edit: Here is my ModelView
[Required (ErrorMessage = "Last Name is required")]
[StringLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
[Display(Name = "Last Name", Prompt = "Attendee Last Name")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[Required(ErrorMessage = "First Name is required")]
[StringLength(50, ErrorMessage = "First Name cannot be longer than 50 characters.")]
[Display(Name = "First Name", Prompt = "Attendee First Name")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessage = "PhoneNumber is required")]
[Display(Name = "Cell Phone")]
[DataType(DataType.PhoneNumber)]
public string CellPhone { get; set; }
[Required(ErrorMessage = "Email is required")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }

MVC Admin reset of User Password

After a lot of messing around, and with some excellent help from ADyson, I got this working.
On my system, when an admin user logs in, a link appears for them to go into the user management system. This provides a list of users, the ability to create another user, delete them, or change their details.
Also, when ANY user logs in they are able to change their own password. However, if a user forgets their password, the admin user must reset the password. I'm not emailing them a link or anything fancy. In the "List Users" bit in the admin screen, there is an Actions column that contains links to edit, delete, show details, and reset password.
I have an ApplicationUsersController that contains the functions to edit, delete, etc. I have a series of ApplicationUsers views called Create, Edit, Delete, Details, Edit, Index. Most of this code was generated when I created an ApplicationUsersController and chose to create the views. There is also a ResetUserPasswordsViewModel as well. Here is the ResetPassword view:
#model ICWeb.Models.ResetUserPasswordViewModel
#{
ViewBag.Title = "Reset User Password";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>#ViewBag.Title</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "Please fix the errors displayed", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.NewPassword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.NewPassword, new { htmlAttributes = new { #class = "form-control", #autofocus = "autofocus" } })
#Html.ValidationMessageFor(model => model.NewPassword, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Reset Password" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
In the controller I have:
// GET: /ApplicationUsers/ResetPassword
public ActionResult ResetPassword(string id)
{
return View(new ResetUserPasswordViewModel() { Id = id });
}
//POST: /ApplicationUsers/ResetPassword
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetPassword(ResetUserPasswordViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
string userId = model.Id;
string newPassword = model.NewPassword;
string hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
ApplicationUser cUser = await store.FindByIdAsync(userId);
await store.SetPasswordHashAsync(cUser, hashedNewPassword);
await store.UpdateAsync(cUser);
return RedirectToAction("Index");
}
After a lot of messing around, I re-did this function. The view loads now and I can type in 2 new passwords. When I submit, the ResetPassword function runs. I can see when I step through the code it has the passwords I typed, and by editing the GET function to populate the model with the Id, I now get the Id of the user. The whole controller access is limited to users with admin permissions, so unless you're an admin you can't do anything here.
In my ResetUserPasswordModel I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ICWeb.Models
{
public class ResetUserPasswordViewModel
{
public string Id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
}
All sorted, and the help was, and is, very much appreciated.
In the system I'm developing (but not yet completed or tested) I've got this written. It works so should be a good starting point. Note that the view model takes care of mis-match passwords so that is covered for you already.
I use Direct Injection for the User Manager - just replace my _userManager with your own instance, however you create it.
#model Models.ResetPasswordViewModel
#{
ViewBag.Title = "Reset password";
}
<div class="container">
#if (ViewBag.Error != null)
{
<div class="alert-danger mb-2">Error(s) occured : #ViewBag.Error</div>
#Html.ActionLink("Back to List", "AllUsers", null, new { #class = "btn btn-outline-primary" })
}
else
{
using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary("", new { #class = "text-danger" })
#Html.HiddenFor(x => x.Id)
<div class="form-group">
#Html.LabelFor(model => model.UserName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.UserName, new { htmlAttributes = new { #class = "form-control", #readonly = "" } })
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "control-label" })
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</div>
<div class="form-group">
#Html.LabelFor(m => m.ConfirmPassword, new { #class = "control-label" })
#Html.PasswordFor(m => m.ConfirmPassword, new { #class = "form-control" })
</div>
<div class="form-group d-flex">
#Html.ActionLink("Back to User Edit", "EditUser", "Account", new { userId = Model.Id }, new { #class = "btn btn-outline-primary" })
<input type="submit" value="Reset Password" class="btn btn-primary ml-auto" />
</div>
}
}
</div>
public class ResetPasswordViewModel
{
[Display(Name = "User Id")]
public string Id { get; set; }
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
[ValidateAntiForgeryToken]
public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = _userManager.FindById(model.Id);
IdentityResult result = null;
if (user != null)
{
string code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
result = await _userManager.ResetPasswordAsync(user.Id, code, model.Password);
if (result.Succeeded)
{
return RedirectToAction("ResetPasswordConfirmation", "Account", model);
}
}
// return errors
var s = new System.Text.StringBuilder();
//
foreach (var e in result.Errors)
{
if (s.Length > 0)
{
s.Append(", ");
}
s.Append(e);
}
ViewBag.Error = s.ToString();
return View();
}

Update password for user account in mvc 5

I'm currently working on user account and so far i've managed to work upon register and login panel with email confirmation process. Here i'm stuck in forget password option. I'm using ado.net entity framework. All i have to do is to change password for the registered email. This is what i've done so far.
Note: getting the error in controller action method
the entity type is not part of the model for the current context
Edit
I have a table named registration attached to the DBContext class. And i'm trying to update the records (particularly the password field) for the forger password option. I made the property class UpdateAcccount.cs with validation as attached below. In order to update the password, I retrieved the row matching with email id. And then transferring the updated password in the database.
This time i'm getting the error of "password does not match" although there's no field of confirm password in the database(registration table) + i even tried to use bind(exclude) attribute for confirm password but that didn't work either.
Controller class
[HttpPost]
public ActionResult UpdateAccount(UpdateAccount account)
{
string message = "";
bool status = false;
if (ModelState.IsValid)
{
account.Password = Crypto.Hash(account.Password);
account.ConfirmPassword = Crypto.Hash(account.ConfirmPassword);
using (TravelGuide1Entities entity = new TravelGuide1Entities())
{
try
{
var v = entity.Registrations.Where(a => a.Email == account.Email).FirstOrDefault();
if (v != null)
{
v.Password = account.Password;
entity.Entry(v).State = System.Data.Entity.EntityState.Modified;
entity.SaveChanges();
return RedirectToAction("Login");
}
}
catch(Exception e)
{
}
}
}
return View();
}
UpdateAccount.cs
public class UpdateAccount
{
[Display(Name = "Email")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email id required")]
public string Email { get; set; }
[Display(Name = "New Password")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Password required")]
[DataType(DataType.Password)]
[MinLength(6, ErrorMessage = "Minimum 6 character required")]
public string Password { get; set; }
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
[Compare("Password", ErrorMessage = "Password do not match")]
public string ConfirmPassword { get; set; }
}
UpdateAccount.cshtml
#model Travel.Models.UpdateAccount
#{
ViewBag.Title = "UpdateAccount";
}
<h2>UpdateAccount</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>UpdateAccount</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</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.ConfirmPassword, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ConfirmPassword, "", 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>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}

how to add phone number field in registeration page of mvc application

i want to add phone number field on default registration page of MVC5 Web Application.
and when a user register with given info the user data store in default database of AspNetUsers table in PhoneNumber column.
here is my code for register View.
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.ConfirmPassword, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.ConfirmPassword, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Number, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Number, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
and here is code for register view model.
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Your must provide a PhoneNumber")]
[Phone]
[Display(Name = "Phone Number")]
public string Number { get; set; }
}
String number is also used in Manage.so i use the same name.but when a user register its phone number is not showing on database table.
All you need to do is include the phone number in the ApplicationUser like this:
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, PhoneNumber = model.Phone };
You can find that object in the
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, PhoneNumber = model.Phone };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
function in the AccountController.cs in a fresh MVC5 project. You can check the AspNetUsers table to see where it went.
It is probably wiser to not store your user data in that table, but create a seperate table for things like phone numbers, etc. That way you can keep your AccountController uncluttered and lean.
For the record, I just did a 'Create new project > MVC' in my VS2012. Nothing was changed from the default settings the new project came with except adding the Phone property to the model and an input field to the view, just like you did.

Why if statement always returns false in asp.net mvc when updating data?

I used this code to update user's data in asp.net mvc 5 I think every thing is OK , but I don't know why I'm getting error message and data didn't saved and if (ModelState.IsValid) in Controller is false always .
Could anyone help me please ?
Admin Controller
[HttpGet]
public ActionResult EditUser(int id)
{
var load = db.Users.Find(id);
return View(load);
}
private const string _ImagesPathUser = "~/Images/User";
[HttpPost]
public ActionResult EditUser(User user, HttpPostedFileBase UploadImage)
{
if (ModelState.IsValid) // always returns false
{
UserRepositories blUser = new UserRepositories();
if (UploadImage != null)
{
// Delete exiting file
System.IO.File.Delete(Path.Combine(Server.MapPath(_ImagesPathUser), user.UserImage));
// Save new file
string fileName = Guid.NewGuid() + Path.GetFileName(UploadImage.FileName);
string path = Path.Combine(Server.MapPath(_ImagesPathUser), fileName);
UploadImage.SaveAs(path);
user.UserImage = fileName;
}
if (blUser.Update(user))
{
return JavaScript("alert(' added');");
}
else
{
return JavaScript("alert(' didn't add');");
}
}
else
{
return JavaScript("alert('Error');");
}
}
UserRepositories.cs
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Users.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Images\\User\\" + entity.UserImage);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}
EditUser.cs
#model NP1.Models.User
#{
ViewBag.Title = "EditUser";
Layout = "~/Views/Admin/AdminLayout.cshtml";
}
<h2>EditUser</h2>
#using (Html.BeginForm("EditUser", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "myUploadForm5" }))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.UserID)
<div class="form-group">
#Html.LabelFor(model => model.UserEmail, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserEmail)
#Html.ValidationMessageFor(model => model.UserEmail)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserFirstName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserFirstName)
#Html.ValidationMessageFor(model => model.UserFirstName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserLastName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserLastName)
#Html.ValidationMessageFor(model => model.UserLastName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserPassWord, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserPassWord)
#Html.ValidationMessageFor(model => model.UserPassWord)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserCellPhone, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserCellPhone)
#Html.ValidationMessageFor(model => model.UserCellPhone)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserTell, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserTell)
#Html.ValidationMessageFor(model => model.UserTell)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserImage, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.ImageFor(model => model.UserImage, new { width = "300" }, "", "Images", "User")
#Html.Upload("UploadImage")
#Html.HiddenFor(model => model.UserImage)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserAddress, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserAddress)
#Html.ValidationMessageFor(model => model.UserAddress)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserBirthDate, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserBirthDate)
#Html.ValidationMessageFor(model => model.UserBirthDate)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserGender, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserGender)
#Html.ValidationMessageFor(model => model.UserGender)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
User.cs
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
UserMetaData.cs
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
[Required(ErrorMessage = "enter email", AllowEmptyStrings = false)]
[DisplayName("email")]
[Display(Name = "email")]
[RegularExpression(#"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$", ErrorMessage = "enter correct email")]
public string UserEmail { get; set; }
[Required(ErrorMessage = "enter your name", AllowEmptyStrings = false)]
[DisplayName("name")]
[Display(Name = "name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserFirstName { get; set; }
[Required(ErrorMessage = "enter your last name", AllowEmptyStrings = false)]
[DisplayName("last name")]
[Display(Name = "last name")]
[StringLength(50, ErrorMessage = "should be 50")]
public string UserLastName { get; set; }
[Required(ErrorMessage = "enter password", AllowEmptyStrings = false)]
[Display(Name = "password")]
[DisplayName("password")]
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
[Display(Name = "mobile")]
[DisplayName("mobile")]
[RegularExpression(#"^0?9[123]\d{8}$", ErrorMessage = "enter mobile correct")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserCellPhone { get; set; }
[Display(Name = "tel")]
[DisplayName("tel")]
[StringLength(11, ErrorMessage = "should be 11")]
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "status")]
[DisplayName("status")]
public Nullable<byte> UserStatus { get; set; }
[Display(Name = "BirthDate")]
[DisplayName("BirthDate")]
public Nullable<System.DateTime> UserBirthDate { get; set; }
[Display(Name = "gender")]
[DisplayName("gender")]
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
[Required(ErrorMessage = "enter your pass", AllowEmptyStrings = false)]
[Display(Name = "repeate pass")]
[DisplayName("repeate pass")]
[DataType(DataType.Password)]
[Compare("UserPassWord", ErrorMessage = "not equal")]
public string UserConfirmPassWord { get; set; }
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
}
For debugging purposes you can add this code to your action:
foreach (ModelState modelState in ViewData.ModelState.Values) {
foreach (ModelError error in modelState.Errors) {
//if you have some kind of logger use it here to get error data
// if not:
var e = error;
}
}
Put a break point on var e = error; line and check this error object for more info
You have few validations on each of the input elements in the model which might not be matching with database constraints.
Please debug the line ModelState.Isvalid, and use a quick watch to drilldown the errors/values details if any.
Alternatively, use below line in controller method to check for errors with model response due to input values
var errors = ModelState.Values.SelectMany(v => v.Errors);
Check if this helps you.

Resources