i want to create users with special features in mvc. when user is going to create i want to assign some special feature to each user like particular user having his own house, h
try with this, in you Model of Feature add a new property
public bool isFeatureOf { get; set; }
also in your model for the method AddNewUser change it to
public void AddNewUser(User userAdd,List features)
{
userService = new UserService(userDbContext);
User = userService.AddUser(userAdd);
userService.SaveUser();
//featureService = new FeatureService(yourdbcontext)
foreach (Feature item in features)
{
//save to db
featureService.SaveFeature(item,User.Id);
//i don't know if in your database you already have a table,colum or something to map the features by user
}
}
then in your view
for(int index=0; index < Model.Features.Count(); index++)
{
@Html.HiddenFor(m=>Model.Features[index].NameFeature)
@Html.Raw(Model.Features[index].NameFeature)
@Html.CheckBoxFor(m=>Model.Features[index].isFeatureOf)
}
also in your view you'll need to change this
@Html.TextBoxFor(m => m.User.UserName)
@Html.ValidationMessageFor(m => m.UserName)
@Html.TextBoxFor(m => m.User.UserAddres)
@Html.ValidationMessageFor(m => m.UserAddres)
to:
@Html.TextBoxFor(m =>Model.User.UserName)
@Html.ValidationMessageFor(m => Model.User.UserName)
@Html.TextBoxFor(m => m.User.UserAddres)
@Html.ValidationMessageFor(m =>Model.User.UserAddres)
in your controller change your param to get the whole Model like this
[HttpPost]
public ActionResult Create(ViewModelUserWithFeature model)
{
if (ModelState.IsValid)
{
model.AddNewUser(model.User,model.Features);
}
return RedirectToAction("Index", viewModelUserWithFeature);
}
hope this can help you