I have a strongly typed view that is using a viewmodel I created. I have a date field in my viewmodel that is a formatted display of a property of my main model. In the POST Edit action (I followed the nerd dinner tutorial) parameters are int id, FormCollection collection
. Using my repository I lookup the model off of ID and I am left to capture my additional viewmodel properties using FormCollection. There must be a better way? Maybe just modify the parameters to accept my viewmodel instead of int id? Why doesn't nerd dinner do this instead of relying on UpdateModel()? Is my UpdateModel incorrect for not picking up the view model properties and updating accordingly?
Example below. ValidateInput(false) is for my WYSIWYG editor
[Authorize(Roles = "Author,Admin"), HttpPost, ValidateInput(false)]
public virtual ActionResult Edit(int id, FormCollection collection)
{
Spotlight spotlight = spotlightRepository.GetSpotlight(id);
try
{
spotlight.ModifiedDate = DateTimeOffset.Now;
// I dont like this line
spotlight.PublishDate = Convert.ToDateTime(collection["PublishDate"]);
UpdateModel(spotlight, "Spotlight");
spotlightRepository.Save();
return RedirectToAction("Details", new { id = spotlight.SpotlightID });
}
catch
{
ModelState.AddRuleViolations(spotlight.GetRuleViolations());
return View(new SpotlightFormViewModel(spotlight));
}
}