我对MVC很陌生, 我首先使用数据库从本地sql server导入表。我使用entityFrameWork并保留所有默认功能。添加控制器和视图后。我回到我的sql server并添加另一个名为DateofBirth(datetime)的列。我去MVC, 我将DateofBirth放在模型和视图中(在创建视图中)。但是,数据会在所有' 1/1/0001 12:00:00 AM +00:00'而不是实际的数据。 可能的原因是什么?
人物模型:
partial class Person
{
public Person()
{
this.appts = new HashSet<appt>();
}
public int P_Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public DateTime DateofBirth { get; set; }
public virtual ICollection<appt> appts { get; set; }
}
人物视图(索引):
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.P_Id)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.DateofBirth)
</th>
<th></th>
</tr>
foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.P_Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateofBirth)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.P_Id }) |
@Html.ActionLink("Details", "Details", new { id=item.P_Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.P_Id })
</td>
</tr>
}
</table>
控制器:
namespace WebApplication2.Controllers
{
[Authorize(Users="aaron")]
public class personController : Controller
{
private dumb_so_dumbEntities db = new dumb_so_dumbEntities();
// GET: /person/
public ActionResult Index()
{
return View(db.Persons.ToList());
}
// GET: /person/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.Persons.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// GET: /person/Create
public ActionResult Create()
{
return View();
}
// POST: /person/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="P_Id,LastName,FirstName,Address,City")] Person person)
{
if (ModelState.IsValid)
{
db.Persons.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: /person/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.Persons.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: /person/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="P_Id,LastName,FirstName,Address,City")] Person person)
{
if (ModelState.IsValid)
{
db.Entry(person).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
// GET: /person/Delete/
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Person person = db.Persons.Find(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
// POST: /person/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Person person = db.Persons.Find(id);
db.Persons.Remove(person);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}