3

I have the following code in my HomeController:

public ActionResult Create()
        {
            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Article articleToCreate)
        {
            if (!ModelState.IsValid)
                return View();

            try
            {
                _db.AddToArticleSet(articleToCreate);
                articleToCreate.posted = DateTime.Now;
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

And this is the view for the Create Method

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <div id="create">

        <h2>Create News Article <span>( <%=Html.ActionLink("Cancel", "Index") %> )</span></h2>

        <% using (Html.BeginForm()) {%>

            <fieldset>
                <legend>Fields</legend>
                <p>
                    <label for="headline">Headline</label>
                    <%= Html.TextBox("headline") %>
                </p>
                <p>
                    <label for="story">Story <span>( HTML Allowed )</span></label>
                    <%= Html.TextArea("story") %>
                </p>
                <p>
                    <label for="image">Image URL</label>
                    <%= Html.TextBox("image") %>
                </p>
                <p>
                    <input type="submit" value="Post" />
                </p>
            </fieldset>

        <% } %>

    </div>
    <!-- END div#create -->

</asp:Content>

The problem I get when I try and submit the data is an InnerException saying that the IDENTITY_INSERT is set to off. However the ID in my article table (which is called storyId) is set to Auto Increment, and is also set as Identity and also as primary key/index.

How do I fix this problem? Thanks.

Victor Haydin
  • 3,518
  • 2
  • 26
  • 41
Cameron
  • 27,963
  • 100
  • 281
  • 483
  • I will :) Once I get the answer I need I will accept with pleasure. – Cameron Jan 05 '11 at 19:33
  • 1
    If you don't get the answer you 'need', then consider revisiting the questions with some feedback in order to improve the response. I've seen at least two of your questions where someone has provided feedback and you simply haven't responded. Also, if it's any help, your HTML is invalid. An input field in a `p` tag?! Consider using `div`! – Dan Atkinson Jan 05 '11 at 21:47

1 Answers1

3

Did you create the Linq To SQL DBML file and then change the Database Schema to all for Auto Increment? I'm thinking that you may have changed the schema but forgot to update the linq designer.

Just delete the table in the designer and re-drag it from the database.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Genius :) I just refreshed it. Accepted your answer too! ;) Think you could help with this one too? http://stackoverflow.com/questions/4608397/asp-net-mvc-sequence-contains-no-elements/4608420#4608420 – Cameron Jan 05 '11 at 19:55
  • I posted my answer to your other question. – Chase Florell Jan 06 '11 at 02:03