Reputation: 28853
I have the following Create Method in my HomeController:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")]Article articleToCreate)
{
if (!ModelState.IsValid)
return View();
try
{
_db.AddToArticleSet(articleToCreate);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
And here is the view:
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="headline">Headline</label>
<%= Html.TextBox("headline") %>
</p>
<p>
<label for="story">Story</label>
<%= Html.TextArea("story") %>
</p>
<p>
<label for="image">Image URL</label>
<%= Html.TextBox("image") %>
</p>
<p>
<input type="submit" value="Create News Story" />
</p>
</fieldset>
<% } %>
However when I hit submit I'm just returned to the form (with the fields still filled in) and the new story isn't created. Any ideas why? Thanks.
Edit: I get the following error in the InnerException {"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."}
Upvotes: 0
Views: 972
Reputation: 29081
Since you're sinking any exception that happens while creating the article, you'll never get an error that happens during this phase. (See Elmah for a very, very easy way to log all of your errors)
Have you tried setting a breakpoint in the method and seeing what's really happening? I'd bet that either your model state is invalid or you're getting an exception.
Slightly related - none of your return methods:
return View();
return RedirectToAction("Index");
// and the last one
return View();
pass any model data to the view, so you won't see any of your field values in the case that there is a validation error.
Upvotes: 0
Reputation: 21098
Under both cases where you return the view you aren't returning in any way what went wrong. So either your Model State is Invalid OR the Save to the DB failed.....
Upvotes: 0
Reputation: 245509
There are only two situations that would cause that to happen. Either your ModelState is invalid:
if(!ModelState.IsValid)
return View();
Or there are Exceptions being thrown when trying to write to the db:
catch { return View(); }
Since you have no validation messages in your View, there's no visual feedback to whether or not something is invalid. There's also no way for me to tell if there are database issues.
I would suggest setting a breakpoint at the beginning of the Action method and stepping through. That will tell you exactly what the issue is.
Upvotes: 3