Reputation: 5832
All,
I have 2 view models (2 different pages) that both include and need a model class (NewClub) that has required attributes but some of these required attributes are not needed until the second view. Basically view #1 updates/inserts some of the properties in model class NewClub and view #2 inserts/updates other properties in model class NewClub.
I tried ignoring the required properties not needed in view #1, in the view #1 controller like this:
ModelState.Remove("NewClub.ClubCounselorContact");
ModelState.Remove("NewClub.ClubCounselorEmail");
ModelState.Remove("NewClub.ClubCounselorPhone");
This makes model state validation pass but when I go to save:
model.NewClub.NewClubType = model.ClubTypeSelected;
db.NewClubs.Add(model.NewClub);
db.Entry(model.NewClub).State = EntityState.Modified;
try
{
var dbResult = db.SaveChanges() > 0;
}
I get:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Is there a way to get around this?
Thanks
Example:
public class NewClub
{
public string ClubCounselorMasterCustomerId { get; set; }
//Used only on view page #2
[Display(Name = "Club counselor")]
[Required(ErrorMessage = "Club counselor name")]
public string ClubCounselorContact { get; set; }
//Used only on view page #2
[Display(Name = "Club counselor email")]
[Required(ErrorMessage = "Club counselor email")]
public string ClubCounselorEmail { get; set; }
//Used only on view page #2
[Display(Name = "Club counselor phone")]
[Required(ErrorMessage = "Club counselor phone")]
public string ClubCounselorPhone { get; set; }
[...]
}
//View Model #1
public class LetsGetStartedViewModel
{
public NewClub NewClub { get; set; }
public bool HasExistingBuildingClubs { get; set; }
[...]
}
//View Model #2
public class FormANewClubTeamViewModel
{
public NewClub NewClub { get; set; }
public List<NewClubSponsor> Sponsors { get; set; }
[...]
}
Upvotes: 0
Views: 3436
Reputation: 14640
The real problem here is that you're not leveraging the power of your view models. This is the exact reason why it's not a good idea to have your domain models in your views, because if your views have different validation requirements, you don't have the flexibility to do anything about it.
So, what I'd suggest is rather than using NewClub
directly in your view models, you add properties to your view models to represent the properties you need for a NewClub
. That way, you can decorate those properties with data annotations for each individual view, meaning your validation rules can change. After that, you map the properties from the view models back to your domain model.
As an example:
public class LetsGetStartedViewModel
{
[Required]
public string ClubCounselorContact { get; set; }
}
public class FormANewClubTeamViewModel
{
public string ClubCounselorContact { get; set; }
}
Then your controller might look like this:
[HttpPost]
public ActionResult SomeAction(LetsGetStartedViewModel model)
{
if (ModelState.IsValid)
{
// map properties onto a NewClub, then add it to db or whatever
NewClub newClub = new NewClub
{
ClubCounselorContact = model.ClubCounselorContact
};
// add to database
return RedirectToAction("Success");
}
return View(model);
}
Upvotes: 4
Reputation: 33149
You write:
required attributes but some of these required attributes are not needed until the second view
This means that your Models are different, so each View should get its own Model; each with the proper Required
markings.
Yes, it requires a copy/paste, but (1) this is by far the simplest solution and (2) it is also conceptually correct: different requirements for different Views means different Models.
Upvotes: 1