I am a beginer in MVC.
What i want is that, a user wil have a Location.
I want to keep all locations in a seprate table.
The models i came up with are:
Location Model:
public class Location
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int LocationId { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
userprofile Model:
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public virtual Location location { get; set; }
}
Register Model:
public class RegisterModel
{
//Attributes for UserName , password and ConfirmPassword
public string Name { get; set; }
public virtual Location Location { get; set; }
}
Register Controller :
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new { Location = new Location () , Name = model.Name });
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Now when i try to register,
Error : No mapping exists from object type HireCar2.Models.Location to a known managed provider native type.
I have been trying on this Since noon.
Can i know what is wrong in the above code.
Related
ApplicationUser Class which inherit with IdentityUser
public class ApplicationUser : IdentityUser
{
public DateTime BirthDate { get; set; }
public string User_type { get; set; }
public DateTime CreatedOn { get; set; }
public Enums.Sex Gender { get; set; }
public bool IsActive { get; set; }
public virtual Patient Patients { get; set; }
public int PatientID { get; set; }
}
Patient Class
public class Patient
{
[Key]
public int PatientID { get; set; }
public string ProfilePicture { get; set; }
public string FName { get; set; }
public DateTime BirthDate { get; set;}
public string City { get; set; }
public string Country { get; set; }
}
Controller
public async Task<ActionResult> RegisterPatient(RegisterViewModel model)
{
MYDb db = new MYDb();
Patient pt = new Patient();
pt.BirthDate = model.BirthDate;
pt.City = model.City;
pt.Country = model.Country;
pt.ProfilePicture = model.ProfilePicture;
pt.FName = model.FName;
int Pat_id = model.PatientID;
if (ModelState.IsValid)
{
ApplicationUser myuser = new ApplicationUser();
model.CreatedOn = DateTime.Now;
model.IsActive = true;
myuser.PatientID = Pat_id;
if (ModelState.IsValid)
{
var user = model.GetUser();
db.Patients.Add(pt);
db.SaveChanges();
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var idManager = new IdentityManager();
idManager.AddUserToRole(user.Id, model.User_type);
//await db.SaveChangesAsync();
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
return RedirectToAction("Index", "Home");
}
// If we got this far, something failed, redisplay form
return View();
}
i want to make relationship b/w Patient and ApplicationUser first i
add record in patient then "Patient_ID" used as a foreign key in
ApplicationUser Table but when add data i face this type of error
Error
Note : data save in Patient table but not in ApplicationUser table because ApplicationUser not get "PatientID" therefore conflict occurred in DB.
I hope this is much clear and its not easy to solve :(
In Applicatin User Model Write done this :
public class ApplicationUser : IdentityUser
{
public DateTime BirthDate { get; set; }
public string User_type { get; set; }
public DateTime CreatedOn { get; set; }
public Enums.Sex Gender { get; set; }
public bool IsActive { get; set; }
[ForeignKey("PatientID")]
public virtual Patient Patients{ get; set; }
}
I think it will work.
Again...
I am doing a MVC with EF5 App. I have a Users Entity, that EF bind with Users table in Database... Looks like this.
public partial class Users
{
public long User_id { get; set; }
[Required]
[StringLength(30, ErrorMessage = "LastName cannot be longer than 30 characters.")]
public string LastName { get; set; }
[Required]
[StringLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")]
public string Name { get; set; }
public int ProcessState_id { get; set; }
public string Sex { get; set; }
[Required,Range(1, int.MaxValue, ErrorMessage = "El País es Obligatorio")]
public int Country_id { get; set; }
[Required]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
public System.DateTime CreationDate { get; set; }
public Nullable<System.DateTime> UpDateTime { get; set; }
[RegularExpression(#"^.{5,}$", ErrorMessage = "Minimum 3 characters required")]
[Required]
[StringLength(9, MinimumLength = 3, ErrorMessage = "Password cannot be longer than 9 characters.")]
public string Password { get; set; }
public string Url { get; set; }
public byte[] Picture { get; set; }
public string CodArea { get; set; }
public string PhoneNumber { get; set; }
public virtual Countries Countries { get; set; }
public virtual ProcessStates ProcessStates { get; set; }
public virtual States States { get; set; }
[NotMapped] // Does not effect with your database
[RegularExpression(#"^.{5,}$", ErrorMessage = "Minimum 3 characters required")]
[StringLength(9, MinimumLength = 3, ErrorMessage = "Confirm Password cannot be longer than 9 characters.")]
[Compare("Password")]
public virtual string ConfirmPassword { get; set; }
}
I have a Model Class that i use it in my Create View....
public class UserViewModel
{
public Users user { get; set; }
public IList<SelectListItem> AvailableCountries { get; set; }
}
My Create Method in the Controller gets a UserViewModel instance...
My Create Method looks like this.
public async Task<ActionResult> Create(UserViewModel model, System.Web.HttpPostedFileBase image = null)
{
try
{
if (ModelState.IsValid)
{
model.user.ProcessState_id = Security.WAITING;
model.user.Rol_id = Security.ROL_PUBLIC;
model.user.CreationDate = DateTime.Now;
model.user.IP = Request.UserHostAddress;
model.user.Url = UserValidation.EncriptacionURL(model.user.Email);
if (image != null)
{
// product.ImageMimeType = image.ContentType;
model.user.Picture= new byte[image.ContentLength];
image.InputStream.Read(model.user.Picture, 0, image.ContentLength);
}
_db.Users.Add(model.user);
_db.SaveChanges();
return RedirectToAction("Create");
}
model.AvailableCountries = GetCountries();
return View(model);
}
catch (RetryLimitExceededException /* dex */)
{
}
return View(model);
}
So far so good.
For my Edit View, i need less properties from User class, so I have a new class with the properties I need. This class is called UserEditView.
public class UserEditView
{
public long User_id { get; set; }
[Required]
[StringLength(30, ErrorMessage = "LastName cannot be longer than 30 characters.")]
public string LastName { get; set; }
[Required]
[StringLength(30, ErrorMessage = "Name cannot be longer than 30 characters.")]
public string Name { get; set; }
[Required, Range(1, int.MaxValue, ErrorMessage = "El País es Obligatorio")]
public int Country_id { get; set; }
[Required]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
public Nullable<System.DateTime> UpDateTime { get; set; }
public byte[] Picture { get; set; }
public string CodArea { get; set; }
public string PhoneNumber { get; set; }
public virtual Countries Countries { get; set; }
}
I also create a new Model for Edit View, called UserEditViewModel and looks like this.
public class UserEditViewModel
{
public UserEditView user { get; set; }
public IList<SelectListItem> AvailableCountries { get; set; }
}
On my Edit method, I use Mapper to bind User entity with UserEditView
public ViewResult Edit(int User_id=3)
{
Users users = _db.Users
.FirstOrDefault(p => p.User_id == User_id);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Users, UserEditView>();
});
IMapper mapper = config.CreateMapper();
UserEditView userEditView = mapper.Map<Users, UserEditView>(users);
var model = new UserEditViewModel
{
user = userEditView,
AvailableCountries = GetCountries(),
};
return View(model);
}
My problem arise when I want to Update the User table.
The Edit method gets UserEditViewModel instance.
public async Task<ActionResult> Edit(UserEditViewModel model, System.Web.HttpPostedFileBase image = null)
{
try
{
if (ModelState.IsValid)
{}
}
}
UserEditViewModel has an instance of UserEditView but I need an instance of Users in order to EF updates Users Table.
I need to Map again?
How can I get a Users Instance?
I add the following Class
public static class AutoMapperBootStrapper
{
public static void BootStrap()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Users, UserEditView>();
cfg.CreateMap<UserEditView, Users>();
});
IMapper mapper = config.CreateMapper();
}
And I add in my Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapperBootStrapper.BootStrap();
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
then in the controller... i do
public ViewResult Edit(int User_id=3)
{
Users users = _db.Users.FirstOrDefault(p => p.User_id == User_id);
UserEditView userEditView = Mapper.Map<Users, UserEditView>(users);
}
But Mapper.Map have an error... it says Mapper is not instantiated.
the problem is because I defined more than one Mapper. If i define just one, it Works fine...
I need to Map again? How can I get a Users Instance?
You could get the User model from your database using the id and then map the properties that you need to be updated from the view model:
[HttpPost]
public ActionResult Edit(UserEditViewModel model, HttpPostedFileBase image = null)
{
if (!ModelState.IsValid)
{
// Validation failed => redisplay the Edit form so that the
// user can correct the errors
return View(model);
}
var user = _db.Users.FirstOrDefault(p => p.User_id == model.user.User_id);
if (user == null)
{
// no user with the specified id has been found in the database =>
// there's nothing to update
return NotFound();
}
// This will map only the properties of the user object that
// are part of the view model
Mapper.Map<Users, UserEditView>(model.user, user);
// at this stage you could manually update some properties that
// have not been mapped such as the uploaded image
// finally persist the changes to the database
_db.SaveChanges();
// redirect to some other action to show the updated users
return RedirectToAction("users");
}
Also the code you have shown in your question:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Users, UserEditView>();
});
IMapper mapper = config.CreateMapper();
This is absolutely NOT something that you should be doing inside a controller action. AutoMapper mappings should be configured only once per application lifetime, ideally when your application starts, i.e. for a web application that would be Application_Start in Global.asax. In a controller action you should only use the already configured mappings. I strongly recommend you going through the AutoMapper's documentation for getting better understanding of how to use this framework.
Quote from the documentation:
Where do I configure AutoMapper?
If you're using the static Mapper method, configuration should only
happen once per AppDomain. That means the best place to put the
configuration code is in application startup, such as the Global.asax
file for ASP.NET applications. Typically, the configuration
bootstrapper class is in its own class, and this bootstrapper class is
called from the startup method. The bootstrapper class should call
Mapper.Initialize to configure the type maps.
Here I have a MainController in which I have two actions named Create and PhotoUpload. Here is the code for Create action.
// GET: Main/Create
public ActionResult Create()
{
return View();
}
// POST: Main/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Email,Password,FirstName,LastName,Gender,Birthday,ProfileImage,AboutUser")] User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
Here is the code for PhotoUpload action.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PhotoUpload(PhotoModel model)
{
if (model.PhotoFile.ContentLength > 0)
{
var fileName = Path.GetFileName(model.PhotoFile.FileName);
var filePath = Server.MapPath("/Content/Users/Images");
string savedFileName = Path.Combine(filePath, fileName);
model.PhotoFile.SaveAs(savedFileName);
}
return View(model);
}
public ActionResult PhotoUpload()
{
return View();
}
And these are the User and Photo models. This is the User Model
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Friends = new HashSet<Friend>();
this.Friends1 = new HashSet<Friend>();
this.Photos = new HashSet<Photo>();
}
public int UserId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public System.DateTime Birthday { get; set; }
public string ProfileImage { get; set; }
public string AboutUser { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Friend> Friends { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Friend> Friends1 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Photo> Photos { get; set; }
}
This is the PhotoModel
public class PhotoModel
{
[Required]
public HttpPostedFileBase PhotoFile { get; set; }
}
And this is what I am getting as a view now. This is my /Main/Create View
And this is my /Main/PhotoUpload View
Now I want to put this PhotoUpload view instead of ProfileImage thing inside my Create View. Where do I change this and how?
You should use a ViewModel as that's the recommended practice for transferring data to and from views, in this case you can do the following as #StephenMuecke commented
ViewModel
public class UserViewModel
{
public string Email { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public System.DateTime Birthday { get; set; }
public string ProfileImage { get; set; }
public string AboutUser { get; set; }
[Required]
public HttpPostedFileBase PhotoFile { get; set; }
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(UserViewModel model)
{
if (ModelState.IsValid)
{
AddUser(model);
SavePhoto(model.PhotoFile);
return RedirectToAction("Index");
}
return View(user);
}
private void SavePhoto(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var filePath = Server.MapPath("/Content/Users/Images");
string savedFileName = Path.Combine(filePath, fileName);
file.SaveAs(savedFileName);
}
}
private void AddUser(UserViewModel model)
{
var user = new User
{
Email = model.Email, Password = model.Password, FirstName = model.FirstName, LastName = model.LastName, Gender = model.Gender, Birthday = model.Birthday, ProfileImage = model.ProfileImage, AboutUser = model.AboutUser
};
db.Users.Add(user);
db.SaveChanges();
}
For further reading:
http://www.mikesdotnetting.com/article/188/view-model-design-and-use-in-razor-views
http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp-net-mvc-applications/
I'm using ASP.Net MVC 5 from Visual Studio. I want to create a user profile with complex types. I have modified the code of the User class in IdentityModels.cs file. Here's the code:
public class User : IUser
{
public User()
: this(String.Empty)
{
}
public User(string userName)
{
UserName = userName;
Id = Guid.NewGuid().ToString();
}
[Key]
public string Id { get; set; }
public string UserName { get; set; }
public string Phone { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
I've also changed the views for this model. This works great without any problems. However, if I change the type of the Address property above to Address, meaning: public Address Address { get; set; } it fails.
I've tried using the virtual keyword for it but it didn't work. Please note that every time I create the database tables from scratch. Also, I checked the database and the information is inserted into database with correct foreign keys but I don't know what the problem is.
The code execution fails in the code below in the line await Users.Create(user) which returns false:
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
try
{
// Create a profile, password, and link the local login before signing in the user
User user = new User(model.UserName)
{
UserAddress = model.Address,
Email = model.Email,
Phone = model.Phone,
MobilePhone = model.MobilePhone
};
if (await Users.Create(user) &&
await Secrets.Create(new UserSecret(model.UserName, model.Password)) &&
await Logins.Add(new UserLogin(user.Id, IdentityConfig.LocalLoginProvider, model.UserName)))
{
await SignIn(user.Id, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError(String.Empty, "Failed to create login for: " + model.UserName);
}
}
catch (DbEntityValidationException e)
{
ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Update:
Here's the Address class in case you wonder:
public class Address
{
public int ID { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string PostalCode { get; set; }
}
Update 2
Here's the image of the entered data:
It seems that this is a bug. I won't select this as the answer until it is absolutely proven. However when I change my controller's code from:
if (await Users.Create(user) &&
await Secrets.Create...
to:
await Users.Create(user);
if (await Secrets.Create...
it works without any problems. Seems to me that this should be a bug since I can load and edit the data perfectly.
This means that for some reason, even if the IUserStore.Create succeeds, it returns false in case the model is a complex type.
The problem that you have here (at least specific to changing Address to be an object rather than a string), is that your models aren't built correctly to relate them properly. Here is what you should be looking at.
public class User : IUser
{
public User()
: this(String.Empty)
{
}
public User(string userName)
{
UserName = userName;
Id = Guid.NewGuid().ToString();
}
[Key]
public string Id { get; set; }
public string UserName { get; set; }
public string Phone { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
// This FK doesn't need to explicitly be declared, but I do so as it helps me
// with the understanding of my structure a bit better.
public int AddressId { get; set; }
public Address Address { get; set; }
}
You also need to relate your Address back to your User class. I'm not sure how you want to do that, but, assuming that multiple people can live at the same address, you'll want a one-to-many relationship. (Right now, you receive an error because you don't specify the relationship.)
You have to do this in the Address model:
public class Address
{
[Key]
public int ID { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Street { get; set; }
[Required]
public string PostalCode { get; set; }
// I would give this a better property name than "Users" but just putting
// this here for now.
public virtual ICollection<User> Users { get; set; }
}
This way, when your database builds, Entity Framework can now properly build the relationships (where, before, it couldn't tell what you intended - hence the error when you switch over to Address).
Of course, there may be other issues, but, this is one that would cause problems.
I have these classes for my Company Entity
public class Company
{
public Company()
{
this.Users = new HashSet<User>();
this.Tools = new HashSet<Tool>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Tool> Tools { get; set; }
[Required]
public virtual CompanyGroup CompanyGroup { get; set; }
}
public class CompanyDto
{
public string Name { get; set; }
public string Address { get; set; }
}
in the CompaniesController i've added an additional class with the Id parameter for usage in edit/update methods like this
public class CompanyViewEditModel : CompanyDto {
public int Id { get; set; }
}
The Edit Method looks like that:
[HttpPost]
public ActionResult Edit(CompanyViewEditModel companyViewModel)
{
if (ModelState.IsValid)
{
var company = db.Companies.Find(companyViewModel.Id);
company.InjectFrom(companyViewModel);
db.Entry(company).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(companyViewModel);
}
The InjectFrom Method is from ValueInjecter.
Everything works as expected only the db.SaveChanges call fails, because of an validation exception. With some digging in the exceptionI've found out that he thinks the company is invalid because the field CompanyGroup is required but if i take a debugger look at the company variable even after the InjectFrom call everything seems to be fine. The corresponding company group is there.
Consider changing your model as you shouldn't have to mark the navigational property as required. Instead add a FK like so.
public class Company
{
public Company()
{
this.Users = new HashSet<User>();
this.Tools = new HashSet<Tool>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int CompanyGroupId { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Tool> Tools { get; set; }
public virtual CompanyGroup CompanyGroup { get; set; }
}