code first Entity Framework not saving to database - c#

Ok, I am doing a project with entity framework 6. I have my class laid out. When I try to add the information to the database; it gives me following errors:
The best overloaded method match for 'System.Data.Entity.DbSet<img_site_codefi.DAL.DefaultConnection>.Add(img_site_codefi.DAL.DefaultConnection)' has some invalid arguments
Argument 1: cannot convert from 'AnonymousType#1' to 'img_site_codefi.DAL.DefaultConnection'
Here is my controller:
public ActionResult Contact(customer cust)
{
try
{
if (ModelState.IsValid)
{
cust.Tele_comp();
saveIntoDb(cust); // database
SendMail(cust); // mail sender
return RedirectToAction("Submited", "Home");
}
return null;
}
catch (DataException)
{
return View(cust);
}
}
private void saveIntoDb(customer cust)
{
using (var cust_In = new DefaultConnection())
{
var customer = new {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };
//cust_In.customers.Add(customer); //HERE IS THE ERROR!!!
cust_In.SaveChanges();
}
}
and here is the model:
[Key]
[] // how to assign a number automatically
public int Cust_Id { get; set; }
[Required(ErrorMessage = "first name is required!")]
[Display(Name = "First name")]
public string fname { get; set; }
[Display(Name = "Last name")]
public string lname { get; set; }
[Required(ErrorMessage = "area code is required!")]
[StringLength(3)]
[RegularExpression(#"^[0-9]{3,}$", ErrorMessage = "Minimum 3 numbers required & contain only numbers")]
[Display(Name = "Telephone")]
public string tel_area { get; set; }
[Required(ErrorMessage = "first three numbers are required!")]
[StringLength(3)]
[RegularExpression(#"^[0-9]{3,}$", ErrorMessage = "Minimum 3 numbers required & contain only numbers")]
public string fir_thr_tel { get; set; }
[Required(ErrorMessage = "last four numbers are required!")]
[StringLength(4)]
[RegularExpression(#"^[0-9]{4,}$", ErrorMessage = "Minimum 4 numbers required & contain only numbers")]
public string lst_fur_tel { get; set; }
[Required(ErrorMessage = "E-mail is required!")]
[RegularExpression("^[a-zA-Z0-9_\\.-]+#([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$", ErrorMessage = "E-mail is not valid")]
[Display(Name = "Email")]
public string email { get; set; }
[Required(ErrorMessage = "A reason is required!")]
[Display(Name = "Reason")]
public string reasn { get; set; }
public string tele { get; set; }
Also, how do I generate a number automatically for the "Cust_Id" like a database do with the sql code IDENTITY or computed.

You have 2 problems. First, this line is wrong:
var customer = new {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };
You are creating an anonymous type instead of a customer object. Try this instead:
var customer = new customer
{
fname = cust.fname,
lname = cust.lname,
tele = cust.tele,
email = cust.email,
reasn = cust.reasn
};
Secondly your context DefaultConnection is wrong and contains this:
public DbSet<DefaultConnection> customers { get; set; }
You are creating a DbSet of your context class instead of customers. This should be:
public DbSet<customer> customers { get; set; }

You cannot add Anonymous typed class or Dynamic to a DbSet so you need to create an instance class of customer in order to be added to your DbSet.
public ActionResult Contact(Customer cust)
{
try
{
if (ModelState.IsValid)
{
cust.Tele_comp();
saveIntoDb(cust); // database
SendMail(cust); // mail sender
return RedirectToAction("Submited", "Home");
}
return null;
}
catch (DataException)
{
return View(cust);
}
}
private void saveIntoDb(Customer cust)
{
using (var cust_In = new DbContext())
{
var customer = new Customer {fname = cust.fname,lname = cust.lname, tele = cust.tele, email = cust.email, reasn = cust.reasn };
cust_In.Customers.Add(customer); //HERE IS THE ERROR!!!
cust_In.SaveChanges();
}
}
Also your DbContext.cs class should have this instead of your code:
public DbSet<Customer> Customers { get; set; }
For the generation of primary key you should use this:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Make sure you try this tutorial first:
http://msdn.microsoft.com/en-us/data/jj572366.aspx

Related

how update two tables from Edit View ASP.NET MVC?

I am trying to update data from two tables; products and inventory. The main key of the table products is cod_prod, which is the barcode of a product. This is the relationship with the products table and the other. The update is carried out for all the fields, but in the database administrator, the cod_prod field in the inventory table is not updated, it only becomes null, in the products table the update is carried out, the reg_date field, which is a field in the inventory table is also updated. Only the cod_prod field on the inventory table is not updated and I don't know why.
ViewModel:
public class products
{
[Display(Name = "Name")]
public string name { get; set; }
[Key]
[Display(Name = "Product Code")]
public string cod_prod { get; set; }
[Display(Name = "Register Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? reg_date { get; set; }
}
Controller:
[HttpGet]
public ActionResult prodEdit(int id)
{
using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
var u = dc.products.Where(a => a.id == id).FirstOrDefault();
if (u != null)
{
var pm = new products
{
name = u.name,
cod_prod = u.cod_prod,
reg_date = u.reg_date
};
var b = dc.inventory.Where(x => x.cod_prod == pm.cod_prod).FirstOrDefault();
u.cod_prod = b.cod_prod;
return View(u);
}
return Content("Invalid Request");
}
}
[HttpPost]
public ActionResult prodEdit(products prod)
{
using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
var u = dc.products.Where(a => a.id == prod.id).FirstOrDefault();
var b = dc.inventory.Where(x => x.cod_prod == prod.cod_prod).FirstOrDefault();
inventory bod = new inventory()
{
cod_prod = prod.cod_prod,
reg_date = prod.reg_date
};
dc.inventory.Remove(b);
dc.inventory.Add(bod);
dc.products.Remove(u);
dc.products.Add(prod);
dc.SaveChanges();
return RedirectToAction("prodList", "products");
}
}
Any suggestion is appreciated.
UPDATE:
Model for products:
public partial class products
{
[Display(Name = "Name")]
public string name { get; set; }
[Key]
[Display(Name = "Product Code")]
public string cod_prod { get; set; }
}
Model for inventory:
public partial class inventory
{
[Key]
[Display(Name = "Product Code")]
public string cod_prod { get; set; }
[Display(Name = "Register Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? reg_date { get; set; }
}
Suppose you have one to one relation between Products and Inventory tables, your models will look like this in EF:
Products model
public class Products
{
[Display(Name = "Name")]
public string name { get; set; }
[Key]
[Display(Name = "Product Code")]
public string cod_prod { get; set; }
public virtual Inventory Inventory {get;set;}
}
Inventory model
public class Inventory
{
[Key, ForeignKey("Products")]
[Display(Name = "Product Code")]
public string cod_prod { get; set; }
[Display(Name = "Register Date")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? reg_date { get; set; }
public virtual Products Products {get;set;}
}
Once relation is configured, you can simply do this in the POST method to update product and inventory:
[HttpPost]
public ActionResult prodEdit(Products prod)
{
using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
var product = dc.products.Where(a => a.id == prod.id).FirstOrDefault();
var inventory = product.Inventory;
inventory.cod_prod = prod.cod_prod;
inventory.reg_date = prod.reg_date;
dc.SaveChanges();
return RedirectToAction("prodList", "products");
}
}
You can read more about how to configure EF relation here.
If the same thing happens to someone, this is what I wrote to resolve it, the controller has two post methods, the first removes the fields that were changed, save data base and send the products and inventory objects to the second method, there, adds the new data of the models and save. I had to do this way because the removal of the PK on the products table causes the null thing.
Controller:
[HttpPost]
public ActionResult prodEdit(products prod)
{
using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
var u = dc.products.Where(a => a.id == prod.id).FirstOrDefault();
if(u != null)
{
var pm = new products
{
prod_name = prod.prod_name,
cod_prod = prod.cod_prod,
fecha_ingreso = prod.fecha_ingreso
};
var b = dc.bodega.Where(x => x.cod_prod == u.cod_prod).FirstOrDefault();
if (b != null)
{
inventory inv = new inventory()
{
reg_date = pm.fecha_ingreso,
cod_prod = pm.codigo_prod
};
if (inv.cod_prod != null)
{
dc.inventory.Remove(b);
dc.products.Remove(u);
dc.SaveChanges();
prodEdit2(prod, bod);
}
}
}
return RedirectToAction("prodList", "products");
}
}
[HttpPost]
public ActionResult prodEdit2(products p, inventory i)
{
using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
{
dc.products.Add(p);
dc.inventory.Add(i);
dc.SaveChanges();
return RedirectToAction("prodList", "products");
}
}

error "System.ArgumentNullException: 'Value cannot be null. Parameter name: collection' "" ASP:NET MVC C# Visual Studio 2017 dataTables

I have a dataTable in which I have a view to edit the data.
It works but not when I add in my model some Requirements like:
public class ModelTemplateEmail
{
[Display(Name = "EmailId")]
public int EmailId { get; set; }
[Display(Name = "UserName")][StringLength(20, ErrorMessage = "Do not enter more than 20 characters")]
[MaxLength(20)]
[Required(ErrorMessage = "Please enter a User name")]
public string userName { get; set; }
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Please enter an Email")]
[RegularExpression(#"^\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid.")]
public string Email { get; set; }
[Display(Name = "Title")]
public string Title { get; set; }
[Display(Name = "Description")]
public string description { get; set; }
public List<ModelTemplateEmail> EmailDataList { get; set; }
[Key]
public int languageID { get; set; }
[Display(Name = "Language")]
public string MainLanguage { get; set; }
}
I get an error
System.ArgumentNullException: 'Value cannot be null.
Parameter name: collection'
when debugging in my view in one dropDown list Which is not de data I put the requirements so I don't understand why is this error and how to solve it.
The string that have the error is:
#Html.DropDownListFor(model => model.languageID, new List<SelectListItem>(ViewBag.MainLanguage), new { #class = "form-control" })
The controller of my DropDwnList is:
public void MainLanguagelist()
{
var sqlstring = string.Format("SELECT [languageID], [MainLanguage] FROM [dbo].[Language]");
var myConnection = getconection();
SqlCommand myCommand = new SqlCommand(sqlstring, myConnection);
myCommand.ExecuteNonQuery();
SqlDataAdapter Language = new SqlDataAdapter(myCommand);
DataSet setLanguageData = new DataSet();
Language.Fill(setLanguageData);
ViewBag.MainLanguageList = setLanguageData.Tables[0];
List<SelectListItem> MainLanguageList = new List<SelectListItem>();
foreach (DataRow MainLanguage in ViewBag.MainLanguageList.Rows)
{
MainLanguageList.Add(new SelectListItem {Text = #MainLanguage["MainLanguage"].ToString(),
Value = #MainLanguage["languageID"].ToString()});
}
ViewBag.MainLanguage = MainLanguageList;
try
{
myConnection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Action result:
public ActionResult EditData(int EmailId, string userName, string Title, string Email, string description, int languageID)
{
ModelTemplateEmail Editdata = new ModelTemplateEmail
{
EmailId = EmailId, userName = userName, Email = Email, description = description,
Title = Title, languageID = languageID
};
MainLanguagelist();
return View(Editdata);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditData(ModelTemplateEmail modelEmail)
{
UpdateDataBase(modelEmail.EmailId, modelEmail.userName, modelEmail.Title,
modelEmail.Email,modelEmail.description, modelEmail.languageID);
return View ();
}
I fixed it adding MainLanguagelist(); in my http post method.

NotMapped prop Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

good day ..
i created a model that has an property with [Notmapped] DataAnnotations and i created another class inherit from this model with same property but i add required DataAnnotations the problem is when i delete i got error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
"
My Model :
[Key]
[Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "IDDescription", GroupName = "IDGroupName", Name = "IDName", ShortName = "IDShortName", Prompt = "IDPrompt", Order = 50, ResourceType = typeof(Resources.BaseEntity))]
public long ID { get; set; }
[StringLength(207, ErrorMessageResourceName = "StringTooMuch", ErrorMessageResourceType = typeof(Resources.BaseSlider))]
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = "DetailsRequired", ErrorMessageResourceType = typeof(Resources.BaseSlider))]
[Display(Name = "Description", ResourceType = typeof(Resources.BaseSlider))]
public string Description { get; set; }
[NotMapped]
public string ShortDescription
{
get
{
if (Description.Length <= 207)
{
return Description;
}
return Description.Substring(0, 207);
}
}
[Display(Name = "HasBTN", ResourceType = typeof(Resources.BaseSlider))]
public bool HasBTN { get; set; }
[Display(Name = "Is Image Dark")]
public bool IsDark { get; set; }
[Display(Name = "Link", ResourceType = typeof(Resources.BaseSlider))]
public string Link { get; set; }
[Display(Name ="Slider Type")]
public long SliderTypeID { get; set; }
[NotMapped]
//[ImageValidation(".jpg,.png,.japg", OriginalWidth = 1920, OriginalHeight = 600)]
[Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "ImagePathDescription", Name = "ImagePathName", ResourceType = typeof(Resources.BaseMore))]
public virtual HttpPostedFileBase ImagePathFile { get; set; }
#endregion
#region Relations
public virtual IList<BaseSliderPhotoUpload> Photos { get; set; }
public virtual BaseLookup SliderType { get; set; }
#endregion
public BaseSlider()
{
Photos = new List<BaseSliderPhotoUpload>();
}
and the class i created :
public class BaseSliderCreate : BaseSlider
{
#region Data
[NotMapped]
[Required]
//[ImageValidation(".jpg,.png,.japg", OriginalWidth = 1920, OriginalHeight = 600)]
[Display(AutoGenerateField = true, AutoGenerateFilter = true, Description = "ImagePathDescription", Name = "ImagePathName", ResourceType = typeof(Resources.BaseMore))]
public override HttpPostedFileBase ImagePathFile { get; set; }
#endregion
}
in delete actionresult code :
public ActionResult DeleteConfirmed(Guid id)
{
BaseSlider SliderObject = db.Sliders.Where(x => x.GUID == id && x.Deleted == null).FirstOrDefault();
SliderObject.Deleted = DateTime.Now;
SliderObject.DeletedByID = _CurrentUser.ID;
// Delete All Photos
DeletePhoto DeletePhoto = new DeletePhoto();
var DeletedPhotoName = new List<string>();
foreach (var name in SliderObject.Photos)
{
DeletedPhotoName.Add(name.FileName);
}
if (DeletePhoto.PhotoDeleted("Slider", DeletedPhotoName))
{
try
{
db.SliderPhotos.RemoveRange(SliderObject.Photos);
db.Entry(SliderObject).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception ex)
{
ErrorList.Add(ex.Message);
throw;
}
}
else
{
ErrorList.Add(DeletePhoto.ErrorMessage);
}
ViewBag.ErrorList = ErrorList;
return RedirectToAction("Delete", new { id = SliderObject.GUID });
}
when i save change i got error
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
i checked i found the EntityValidationErrors is that ImagePathFile is required..
thanks for helping my and i apologist for my bad English

Replace parameter in an action filter

I am trying to remove the password captured in the parameters from my action filter and replace it with the word "Removed", in order for the parameters to be stored in the database for logging. The password is stored in a ViewModel (depending on the action). Below is sort of a "pseudo-code" as to what I am trying to achieve.
How would I go about masking/replacing the password to be saved in the database? The main issue I am having is that I do not know how to access the password parameter and change it. I have tried getting it using the actionParams.TryGetValue("model, out value) but the problem is that I do not know the type of value and it changes depending on the action. Also, I am unable to call many methods on actionParams["model"] (such as contains)
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var actionParam = filterContext.ActionParameters;
// Remove the password from the parameters
if (actionParam.ContainsKey("model") && actionParam["model"] != null)
{
// If actionParam["model"].ToLower().Contains("password")
// actionParam["model"]["password"] = "Removed";
// If actionParam["model"].ToLower().Contains("confirm password")
// actionParam["model"]["confirm password"] = "Removed";
}
string str = Json.Encode(filterContext.ActionParameters).Trim();
string par = string.Empty;
if (str.Length > 2)
{
par = str.Substring(1, str.Length - 2).Replace("\"", string.Empty);
}
ActionLog log = new ActionLog()
{
SessionId = filterContext.HttpContext.Session.SessionID,
UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Action = filterContext.ActionDescriptor.ActionName,
ActionParameters = par,
IsPost = request.HttpMethod.ToLower() == "post" ? true : false,
IPAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
UserAgent = request.UserAgent,
ActionDate = filterContext.HttpContext.Timestamp
};
//Store the Audit into the Database
ActionLogContext context = new ActionLogContext();
context.ActionLogs.Add(log);
context.SaveChanges();
// Finishes executing the Action as normal
base.OnActionExecuting(filterContext);
}
Example of possible view models
public class LoginViewModel
{
[Required]
[Display(Name = "User ID")]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage="Letters and Numbers Only")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
}
public class ResetPasswordViewModel
{
[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; }
public string Code { get; set; }
}
Example of possible action parameters
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
One approach would be to use an interface as an abstraction so you are not dealing directly with a ViewModel. First, create some interfaces for the action filter to interact with.
public interface IPassword
{
string Password { get; set; }
}
public interface IConfirmPassword
{
string ConfirmPassword { get; set; }
}
Next, make your ViewModel classes implement those interfaces.
public class LoginViewModel : IPassword
{
[Required]
[Display(Name = "User ID")]
[RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage = "Letters and Numbers Only")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
}
public class ResetPasswordViewModel : IPassword, IConfirmPassword
{
[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; }
public string Code { get; set; }
}
Then it is just a matter of updating your filter code. The filter doesn't need to know anything more about your model other than the fact that it implements IPassword or IConfirmPassword, which it can check with a cast.
Of course, for it to work correctly, you have to restore the original values before executing the action method (or alternatively do the logging after the action is run) so the action method will have the correct values.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var actionParam = filterContext.ActionParameters;
IPassword password = null;
IConfirmPassword confirmPassword = null;
string originalPassword;
string originalConfirmPassword;
// Remove the password from the parameters
if (actionParam.ContainsKey("model") && actionParam["model"] != null)
{
// If the model doesn't implement the interface, the result
// here will be null.
password = actionParam["model"] as IPassword;
confirmPassword = actionParam["model"] as IConfirmPassword;
}
if (password != null)
{
// Store the original value so it can be restored later
originalPassword = password.Password;
password.Password = "Removed";
}
if (confirmPassword != null)
{
// Store the original value so it can be restored later
originalConfirmPassword = confirmPassword.ConfirmPassword;
confirmPassword.ConfirmPassword = "Removed";
}
string str = Json.Encode(filterContext.ActionParameters).Trim();
string par = string.Empty;
if (str.Length > 2)
{
par = str.Substring(1, str.Length - 2).Replace("\"", string.Empty);
}
ActionLog log = new ActionLog()
{
SessionId = filterContext.HttpContext.Session.SessionID,
UserName = (request.IsAuthenticated) ? filterContext.HttpContext.User.Identity.Name : "Anonymous",
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Action = filterContext.ActionDescriptor.ActionName,
ActionParameters = par,
IsPost = request.HttpMethod.ToLower() == "post" ? true : false,
IPAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? request.UserHostAddress,
UserAgent = request.UserAgent,
ActionDate = filterContext.HttpContext.Timestamp
};
//Store the Audit into the Database
ActionLogContext context = new ActionLogContext();
context.ActionLogs.Add(log);
context.SaveChanges();
// Restore the original values
if (password != null)
{
password.Password = originalPassword;
}
if (confirmPassword != null)
{
confirmPassword.ConfirmPassword = originalConfirmPassword;
}
// Finishes executing the Action as normal
base.OnActionExecuting(filterContext);
}

WebSecurity.CreateUserAndAccount propertyValues

I'm writing an mvc 4 c# .net 4.5 website
I want to create a new company object and register a new user that is linked to that company.
My account model is:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string PhoneNumber { get; set; }
public bool MarketingEmailOptin { get; set; }
public bool isDisabled { get; set; }
public virtual Company CompanyICanEdit { get; set; }
}
If i call the following it adds the user fine but has null for the CompanyICanEdit field:
WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
addCompanyViewModel.User.Password,
propertyValues: new
{
FirstName = addCompanyViewModel.User.FirstName,
LastName = addCompanyViewModel.User.LastName,
EmailAddress = addCompanyViewModel.User.EmailAddress,
PhoneNumber = addCompanyViewModel.User.PhoneNumber,
MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
isDisabled = false
});
which i would expect as i am not assigning it anything.
i have tried adding (mycompany is a company object):
WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
addCompanyViewModel.User.Password,
propertyValues: new
{
FirstName = addCompanyViewModel.User.FirstName,
LastName = addCompanyViewModel.User.LastName,
EmailAddress = addCompanyViewModel.User.EmailAddress,
PhoneNumber = addCompanyViewModel.User.PhoneNumber,
MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
isDisabled = false,
CompanyICanEdit = mycompany
});
But i get an error saying it can't match the type.
How do i go about registering the user so that the CompanyICanEdit contains the CompanyId value of mycompany?
Any help will be appreciated. thanks
Never worked out how to do it in 1 go, got round it by the following in the end if anyone has the same problem.
//
// POST: /BusinessManager/ManageCompanies/Add
[HttpPost]
public ActionResult Add(AddCompanyViewModel addCompanyViewModel)
{
if (ModelState.IsValid)
{
// Create company and attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
addCompanyViewModel.User.Password,
propertyValues: new
{
FirstName = addCompanyViewModel.User.FirstName,
LastName = addCompanyViewModel.User.LastName,
EmailAddress = addCompanyViewModel.User.EmailAddress,
PhoneNumber = addCompanyViewModel.User.PhoneNumber,
MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
isDisabled = false
});
db.Companies.Add(addCompanyViewModel.Company);
var newuser = db.UserProfiles.FirstOrDefault(u => u.UserName == addCompanyViewModel.User.UserName);
if (newuser != null)
{
newuser.CompanyICanEdit = addCompanyViewModel.Company;
db.Entry(newuser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("", "New user wasn't added");
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", Mywebsite.Controllers.AccountController.ErrorCodeToString(e.StatusCode));
}
}
return View(addCompanyViewModel);
}

Categories

Resources