Updated
I have recently modified a view to post to its controller a json object rather than a formcollection.
In order to get the unit test for this controller working we set the formvalue provider to a dictionary object to stop the UpdateModel method from throwing nre’s.
The resulting unit test below however, simply does not feel like the right thing to be doing. Any insight into how to rework this would be greatly appreciated.
[HttpPost]
public ActionResult ThemeContent(content model)
{
if (ModelState.IsValid)
{
var content = _contentRepository.GetContent(model.id);
if (content == null)
{
content = new content();
UpdateModel(content);
_contentRepository.Add(content);
_contentRepository.Save();
}
else
{
UpdateModel(content);
_contentRepository.Save();
}
return Json(new
{
redirectUrl = Url.Action("index", "success", new {id = content.id}),
isRedirect = true
});
}
string errorMessage = "{";
foreach (var key in ModelState.Keys)
{
var error = ModelState[key].Errors.FirstOrDefault();
if (error != null)
{
if (errorMessage != "{")
{
errorMessage += ",";
}
errorMessage += (char) 34 + "#" + key + (char) 34 + ":" + (char) 34 + error.ErrorMessage + (char) 34;
}
}
errorMessage += "}";
return Json(new
{
Message = errorMessage,
isRedirect = false
});
}
Thanks in advance.
Updated, as we were incorrectly using the updatemodel method.