首先使用Entity Framework Database(Sql Server 2008)处理ASP.NET MVC 4中的项目。我到处寻找,但我找不到任何解决方案。我面临的情况是:
无法创建新的或更新现有的“实习生”错误消息为: DbUpdateException未由用户代码处理 我尝试使用try / catch异常,但没有帮助。
问题似乎来自我所拥有的下拉列表菜单。在编辑模式下(当它具有值时)不仅不显示显示框,而且不保存数据。它没有做任何事情。 所有自由文本字段都可以。
这是控制器:
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using InternApp.Models;
using System;
using System.Data.Entity.Infrastructure;
namespace InternApp.Controllers
{
public class InternController : Controller
{
private InternshipProgramEntities db = new InternshipProgramEntities();
//
// GET: /Intern/
public ActionResult Index()
{
var interns = db.Interns.Include(i => i.Department).Include(i => i.Intern_Coach).Include(i => i.Location).Include(i => i.Intern_Manager).Include(i => i.School).Include(i => i.Semester).Include(i => i.InternStatu);
return View(interns.ToList());
}
//
// GET: /Intern/Details/5
public ActionResult Details(int id = 0)
{
Intern intern = db.Interns.Find(id);
if (intern == null)
{
return HttpNotFound();
}
return View(intern);
}
//
// GET: /Intern/Create
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName");
ViewBag.CoachID = new SelectList(db.Intern_Coach.OrderBy(c => c.LastName), "InternCoachID", "CoachFullName");
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "BldgName");
ViewBag.ManagerID = new SelectList(db.Intern_Manager.OrderBy(m => m.LastName), "InternManagerID", "ManagerFullName");
ViewBag.SchoolID = new SelectList(db.Schools, "SchoolID", "SchoolName");
ViewBag.SemesterID = new SelectList(db.Semesters, "SemesterID", "SemesterName");
ViewBag.InternStatusID = new SelectList(db.InternStatus, "InternStatusID", "Status");
return View();
}
//
// POST: /Intern/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "InternID, InFirstName, InLastName, PersonalEmail, PersonalPhone, CHSEmail, CHSPhone, InternStatusID, LastStatusChangeDate, SchoolID, SemesterID, Major, PartTimeInterest, FTEAvailDate, GraduationDate, DepartmentID, TeamName, ManagerID, LocationID, ComputerNumber, ITInterests, InternStartDate, InternEndDate, PTStartDate, PTEndDate, FTEStartDate, FTEEndDate, InternSalary, PTSalary, FTESalary, ProgramFeedback, CoachID")]
Intern intern)
{
if (ModelState.IsValid)
{
try
{
db.Interns.Add(intern);
db.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception ex)
{
}
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", intern.DepartmentID);
ViewBag.CoachID = new SelectList(db.Intern_Coach, "InternCoachID", "CoachFullName", intern.CoachID);
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "BldgName", intern.LocationID);
ViewBag.ManagerID = new SelectList(db.Intern_Manager, "InternManagerID", "ManagerFullName", intern.ManagerID);
ViewBag.SchoolID = new SelectList(db.Schools, "SchoolID", "SchoolName", intern.SchoolID);
ViewBag.SemesterID = new SelectList(db.Semesters, "SemesterID", "SemesterName", intern.SemesterID);
ViewBag.InternStatusID = new SelectList(db.InternStatus, "InternStatusID", "Status", intern.InternStatusID);
return View(intern);
}
//
// GET: /Intern/Edit/5
public ActionResult Edit(int id = 0)
{
Intern intern = db.Interns.Find(id);
if (intern == null)
{
return HttpNotFound();
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", intern.DepartmentID);
ViewBag.CoachID = new SelectList(db.Intern_Coach, "InternCoachID", "UserName", intern.CoachID);
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "BldgName", intern.LocationID);
ViewBag.ManagerID = new SelectList(db.Intern_Manager, "InternManagerID", "UserName", intern.ManagerID);
ViewBag.SchoolID = new SelectList(db.Schools, "SchoolID", "SchoolName", intern.SchoolID);
ViewBag.SemesterID = new SelectList(db.Semesters, "SemesterID", "SemesterName", intern.SemesterID);
ViewBag.InternStatusID = new SelectList(db.InternStatus, "InternStatusID", "Status", intern.InternStatusID);
return View(intern);
}
//
// POST: /Intern/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(
[Bind(Include = "InternID, InFirstName, InLastName, PersonalEmail, PersonalPhone, CHSEmail, CHSPhone, InternStatusID, LastStatusChangeDate, SchoolID, SemesterID, Major, PartTimeInterest, FTEAvailDate, GraduationDate, DepartmentID, TeamName, ManagerID, LocationID, ComputerNumber, ITInterests, InternStartDate, InternEndDate, PTStartDate, PTEndDate, FTEStartDate, FTEEndDate, InternSalary, PTSalary, FTESalary, ProgramFeedback, CoachID")]
Intern intern)
{
try
{
if (ModelState.IsValid)
{
db.Entry(intern).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DbUpdateException editEx)
{
}
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DepartmentName", intern.DepartmentID);
ViewBag.CoachID = new SelectList(db.Intern_Coach, "InternCoachID", "UserName", intern.CoachID);
ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "BldgName", intern.LocationID);
ViewBag.ManagerID = new SelectList(db.Intern_Manager, "InternManagerID", "UserName", intern.ManagerID);
ViewBag.SchoolID = new SelectList(db.Schools, "SchoolID", "SchoolName", intern.SchoolID);
ViewBag.SemesterID = new SelectList(db.Semesters, "SemesterID", "SemesterName", intern.SemesterID);
ViewBag.InternStatusID = new SelectList(db.InternStatus, "InternStatusID", "Status", intern.InternStatusID);
return View(intern);
}
以下是创建视图:
@model InternApp.Models.Intern
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
var u = new InternApp.Classes.Utilities();
}
<h2 style="margin-left:inherit">Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>New Intern</legend>
<div class="form-inline">
<div class="form-group" >
@* @Html.HiddenFor(model => model.InternID)*@
@Html.Label("First Name")
@Html.EditorFor(model => model.InFirstName)
@Html.ValidationMessageFor(model => model.InFirstName)
</div>
<div class="form-group">
@Html.LabelFor(model => model.InLastName)
@Html.EditorFor(model => model.InLastName)
@Html.ValidationMessageFor(model => model.InLastName)
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.PersonalEmail)
@Html.EditorFor(model => model.PersonalEmail)
@Html.ValidationMessageFor(model => model.PersonalEmail)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PersonalPhone)
@Html.EditorFor(model => model.PersonalPhone)
@Html.ValidationMessageFor(model => model.PersonalPhone)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.CHSEmail)
@Html.EditorFor(model => model.CHSEmail)
@Html.ValidationMessageFor(model => model.CHSEmail)
</div>
<div class="form-group">
@Html.LabelFor(model => model.CHSPhone)
@Html.EditorFor(model => model.CHSPhone)
@Html.ValidationMessageFor(model => model.CHSPhone)
</div>
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.Hidden("InternStatusID")
@Html.Label("Status: ")
@* @Html.DropDownList("InternStatusID", String.Empty) *@
@Html.DropDownListFor(model => model.InternStatu.Status, new SelectList(u.StatusDropdown(), "Value", "Text"))
@Html.ValidationMessageFor(model => model.InternStatu.Status)
</div>
<div class="form-group">
@Html.Label("Last Status Change Date")
@Html.TextBoxFor(model => model.LastStatusChangeDate)
@Html.ValidationMessageFor(model => model.LastStatusChangeDate)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.Hidden("SchoolID")
@Html.Label("School")
@Html.DropDownListFor(model => model.School.SchoolName, new SelectList(u.SchoolDropdown(), "Value", "Text"))
@Html.ValidationMessageFor(model => model.School.SchoolName)
</div>
<div class="form-group">
@Html.LabelFor(model => model.SemesterID, "Semester")
@Html.DropDownList("SemesterID", String.Empty)
@Html.ValidationMessageFor(model => model.SemesterID)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.Major)
@Html.EditorFor(model => model.Major)
@Html.ValidationMessageFor(model => model.Major)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PartTimeInterest)
@Html.EditorFor(model => model.PartTimeInterest)
@Html.ValidationMessageFor(model => model.PartTimeInterest)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.FTEAvailDate)
@Html.TextBoxFor(model => model.FTEAvailDate)
@Html.ValidationMessageFor(model => model.FTEAvailDate)
</div>
<div class="form-group">
@Html.LabelFor(model => model.GraduationDate)
@Html.TextBoxFor(model => model.GraduationDate)
@Html.ValidationMessageFor(model => model.GraduationDate)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.HiddenFor(model => model.DepartmentID)
@Html.Label("Department")
@Html.DropDownList("DepartmentID", String.Empty)
</div>
<div class="form-group">
@Html.LabelFor(model => model.TeamName)
@Html.EditorFor(model => model.TeamName)
@Html.ValidationMessageFor(model => model.TeamName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ManagerID, "Intern_Manager")
@Html.DropDownList("ManagerID", String.Empty)
@Html.ValidationMessageFor(model => model.ManagerID)
</div>
<div class="form-group">
@Html.LabelFor(model => model.LocationID, "Location")
@Html.DropDownList("LocationID", String.Empty)
@Html.ValidationMessageFor(model => model.LocationID)
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.ComputerNumber)
@Html.EditorFor(model => model.ComputerNumber)
@Html.ValidationMessageFor(model => model.ComputerNumber)
</div>
<div class="form-group">
@Html.LabelFor(model => model.ITInterests)
@Html.EditorFor(model => model.ITInterests)
@Html.ValidationMessageFor(model => model.ITInterests)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.InternStartDate)
@Html.TextBoxFor(model => model.InternStartDate)
@Html.ValidationMessageFor(model => model.InternStartDate)
</div>
<div class="form-group">
@Html.LabelFor(model => model.InternEndDate)
@Html.TextBoxFor(model => model.InternEndDate)
@Html.ValidationMessageFor(model => model.InternEndDate)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.PTStartDate)
@Html.TextBoxFor(model => model.PTStartDate)
@Html.ValidationMessageFor(model => model.PTStartDate)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PTEndDate)
@Html.TextBoxFor(model => model.PTEndDate)
@Html.ValidationMessageFor(model => model.PTEndDate)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.FTEStartDate)
@Html.TextBoxFor(model => model.FTEStartDate)
@Html.ValidationMessageFor(model => model.FTEStartDate)
</div>
<div class="form-group">
@Html.LabelFor(model => model.FTEEndDate)
@Html.TextBoxFor(model => model.FTEEndDate)
@Html.ValidationMessageFor(model => model.FTEEndDate)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.InternSalary)
@Html.EditorFor(model => model.InternSalary)
@Html.ValidationMessageFor(model => model.InternSalary)
</div>
<div class="form-group">
@Html.LabelFor(model => model.PTSalary)
@Html.EditorFor(model => model.PTSalary)
@Html.ValidationMessageFor(model => model.PTSalary)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.FTESalary)
@Html.EditorFor(model => model.FTESalary)
@Html.ValidationMessageFor(model => model.FTESalary)
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProgramFeedback)
@Html.EditorFor(model => model.ProgramFeedback)
@Html.ValidationMessageFor(model => model.ProgramFeedback)
</div>
</div>
<div class="form-inline">
<div class="form-group">
@Html.LabelFor(model => model.CoachID, "Intern_Coach")
@Html.DropDownList("CoachID", String.Empty)
@Html.ValidationMessageFor(model => model.CoachID)
</div>
</div>
<div id="fUpload">
@Html.Label("Attach a File")
<input type="file" name="fileUpload" id="fileUpload" />
</div>
<br />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div style="margin-left:inherit" >
@Html.ActionLink("Back to List", "Index")
</div>
模特:
namespace InternApp.Models
{
using System;
using System.Collections.Generic;
public partial class Intern
{
public int InternID { get; set; }
public string InFirstName { get; set; }
public string InLastName { get; set; }
public string InternFullName { get { return InFirstName + " " + InLastName; } }
public string PersonalEmail { get; set; }
public string PersonalPhone { get; set; }
public string CHSEmail { get; set; }
public string CHSPhone { get; set; }
public int InternStatusID { get; set; }
public System.DateTime LastStatusChangeDate { get; set; }
public int SchoolID { get; set; }
public Nullable<int> SemesterID { get; set; }
public string Major { get; set; }
public Nullable<bool> PartTimeInterest { get; set; }
public Nullable<System.DateTime> FTEAvailDate { get; set; }
public Nullable<System.DateTime> GraduationDate { get; set; }
public Nullable<int> DepartmentID { get; set; }
public string TeamName { get; set; }
public Nullable<int> ManagerID { get; set; }
public Nullable<int> LocationID { get; set; }
public string ComputerNumber { get; set; }
public string ITInterests { get; set; }
public Nullable<System.DateTime> InternStartDate { get; set; }
public Nullable<System.DateTime> InternEndDate { get; set; }
public Nullable<System.DateTime> PTStartDate { get; set; }
public Nullable<System.DateTime> PTEndDate { get; set; }
public Nullable<System.DateTime> FTEStartDate { get; set; }
public Nullable<System.DateTime> FTEEndDate { get; set; }
public Nullable<decimal> InternSalary { get; set; }
public Nullable<decimal> PTSalary { get; set; }
public Nullable<decimal> FTESalary { get; set; }
public string ProgramFeedback { get; set; }
public Nullable<int> CoachID { get; set; }
public virtual Department Department { get; set; }
public virtual Intern_Coach Intern_Coach { get; set; }
public virtual Location Location { get; set; }
public virtual Intern_Manager Intern_Manager { get; set; }
public virtual School School { get; set; }
public virtual Semester Semester { get; set; }
public virtual InternStatu InternStatu { get; set; }
public virtual Intern Intern1 { get; set; }
public virtual Intern Intern2 { get; set; }
}
}