0

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."}

0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It happens at the catch part and says an error occurred while updating the entries. See the innerexception for details.
I get this error: {"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."}
@Cameron - Considering you don't have a date selection field in your View and you're not setting one in you controller, I'd say that is your problem.
Well I just want to set the posted value to the current date. How do I do that? Thanks.
articleToCreate.WhateverThePropertyIs = DateTime.Now;
0

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.....

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.