我首先使用Entity Framework代码创建了一个数据库。但是当Create视图中的表单发布到(post)Create动作时,每个项目对象的id始终为0.因此,Model.State始终为false。所以我无法将项目类的对象保存到数据库中。我尝试使用[DatabaseGenerated(DatabaseGeneratedOption.Identity)]增加id。但它仍然需要零。
创建操作(帖子)中的错误消息是“projectID字段是必需的。”
我的Project类:
public class Project
{
[Column(Order = 100)]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int projectID { get; set; }
public string projectName { get; set; }
public string descriptionProject { get; set; }
public Project parentProject { get; set; }
}
我的ProjectsController:
// GET: Projects/Create
public ActionResult Create()
{
var projectList = db.Projects.ToList().Select(
c => new SelectListItem
{
Selected = false,
Text = c.projectName,
Value = c.projectID.ToString()
}
).ToList();
ViewData["ProjectList"] = projectList;
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "projectID,projectName,descriptionProject,identifier,date")] Project project)
{
var projectList = db.Projects.ToList().Select(
c => new SelectListItem
{
Selected = false,
Text = c.projectName,
Value = c.projectID.ToString()
}
).ToList();
ViewData["ProjectList"] = projectList;
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
我的创建视图:
@model BulguTakipSistemiv2.Models.Project
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<form class="form-horizontal">
<fieldset>
<legend>Project</legend>
<div class="form-group">
@Html.LabelFor(model => model.projectName, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
<p>@Html.EditorFor(model => model.projectName, new { @class = "form-control" })</p>
@Html.ValidationMessageFor(model => model.projectName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.descriptionProject, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
<p>@Html.EditorFor(model => model.descriptionProject, new { @class = "form-control" })</p>
@Html.ValidationMessageFor(model => model.descriptionProject)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.parentProject, new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
<p>@Html.DropDownListFor(model => model.projectID, (IEnumerable<SelectListItem>)ViewData["ProjectList"], " --Select Parent Project--")</p>
@Html.ValidationMessageFor(model => model.parentProject)
</div>
</div>
<div class="form-group">
<p class="col-lg-12">
<input type="submit" value="Create" class="btn btn-primary btn-lg" />
</p>
</div>
</fieldset>
</form>
}
提前致谢。另外我的连接字符串如下:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-BulguTakipSistemiv2-20140812112012yeni6;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-BulguTakipSistemiv2-20140812112012.mdf" providerName="System.Data.SqlClient" />
<add name="DBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=BulguTakipSistemiv2yeni6;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
答案 0 :(得分:0)
因为您将主键值设置为数据库职责,在创建实体并将其添加为数据行后我的意思是在数据库中(db.SaveChanges();)获取它的值后请删除任何属性来自您的商务舱
答案 1 :(得分:0)
更改此
@Html.DropDownListFor(model => model.projectID,
进入
@Html.DropDownListFor(model => model.parentProject.projectID,