我在我的HomeController下面的代码:ASP.NET MVC IDENTITY_INSERT设置为OFF
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();
}
}
这是create方法
<%@ 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>
我得到这个问题的看法时,我尝试并提交数据是一个InnerException,说IDENTITY_INSERT设置为off。但是,我的文章表(称为storyId)中的ID被设置为自动递增,并且也设置为标识并且还被设置为主键/索引。
我该如何解决这个问题?谢谢。
我会:)一旦我得到了答案,我需要我会很高兴地接受。 – Cameron 2011-01-05 19:33:44
如果你没有得到你需要的答案,那么考虑用一些反馈来重新考虑问题,以改善答案。我已经看到至少有两个问题,其中有人提供了反馈,而您根本没有回应。此外,如果有任何帮助,您的HTML无效。 “p”标签中的输入字段?!考虑使用'div'! – 2011-01-05 21:47:05