The code below is not showing my errors in the view, how do I ensure my errors display in the view?
//Note I throw a rules exception if something goes wrong somewhere, if so I copy the errors onto the modelstate
[HttpPost]
public RedirectToRouteResult TaskDueDate(int id, int taskid)
{
var duedate = Request.Form["duedate"];
var duetime = Request.Form["duetime"];
try
{
var newduedate = DateHelper.GoodDate(duedate, duetime);
_service.SetTaskDueDate(id, taskid, newduedate);
this.FlashInfo("success, task due date has been updated...");
}
catch (RulesException ex)
{
ex.CopyTo(ModelState);
}
return RedirectToAction("TaskDetail");
}
ex.CopyTo extension method:
public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate)
{
CopyTo(ex,modelstate,null);
}
public static void CopyTo(this RulesException ex, ModelStateDictionary modelstate, string prefix)
{
prefix = string.IsNullOrEmpty(prefix) ? "" : prefix + ".";
foreach (var propertyerror in ex.Errors)
{
string key = ExpressionHelper.GetExpressionText(propertyerror.Property);
modelstate.AddModelError(prefix + key, propertyerror.Message);
}
}
In my view I basically have:
<div id="Errors">
<span id="ServerResponse"></span>
<%= Html.ValidationSummary(false, "")%>
</div>
I think the model state gets cleared and no errors remain on a redirect???
Yes, a redirect is a response to the browser that tells it to make a new request, so any context that you have is lost.
You could show an error page when there is anything to display on it:
You would need to change the return type of your action method to the base class
AcionResultto return different kinds of action results.