所以我使用的是一个用户界面,用户应该能够在其中编辑和删除数据库中的数据。但是当我试图发布表单时,它不会保存更改。这里有一些代码:
型号:
namespace Aviato.Models
{
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class TimesheetEntry
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string ProjectId { get; set; }
[Key]
[Column(Order = 2, TypeName = "date")]
public DateTime EntryDate { get; set; }
public decimal HoursWorked { get; set; }
public virtual Project Project { get; set; }
public virtual User User { get; set; }
}
}
视图:
(索引)
@model IEnumerable<Aviato.Models.TimesheetEntry>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Tidrapportering</h1>
<p>
@Html.ActionLink("Skapa ny", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserId)
</th>
<th>
@Html.DisplayNameFor(model => model.Project.ProjectName)
</th>
<th>
@Html.DisplayNameFor(model => model.EntryDate)
</th>
<th>
@Html.DisplayNameFor(model => model.HoursWorked)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project.ProjectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EntryDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.HoursWorked)
</td>
<td>
@Html.ActionLink("Redigera", "Edit", new { id=item.UserId, item.ProjectId, item.EntryDate }) |
@Html.ActionLink("Ta bort", "Delete", new { id=item.UserId, item.ProjectId, item.EntryDate})
</td>
</tr>
}
</table>
<div>
@Html.ActionLink("Tillbaka", "Index", "User")
</div>
(编辑)
@model Aviato.Models.TimesheetEntry
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Redigera</h1>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.UserId)
@Html.HiddenFor(model => model.Project)
@Html.HiddenFor(model => model.User)
<div class="form-group">
@Html.LabelFor(model => model.ProjectId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProjectId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EntryDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EntryDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EntryDate)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.HoursWorked, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HoursWorked, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.HoursWorked)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Spara" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Tillbaka", "Index")
</div>
控制器:
public ActionResult Edit(int? id, string projectId, DateTime entryDate)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var timesheetentry = _db.TimesheetEntries.Find(id, projectId, entryDate);
if (timesheetentry == null)
{
return HttpNotFound();
}
ViewBag.ProjectId = new SelectList(_db.Projects, "ProjectId", "ProjectName", timesheetentry.ProjectId);
ViewBag.UserId = new SelectList(_db.Users, "UserId", "SocialSecurityNumber", timesheetentry.UserId);
return View(timesheetentry);
}
[HttpPost]
public ActionResult Edit(TimesheetEntry timesheetentry)
{
if (ModelState.IsValid) // So here's where the breakpoint skips. I get User and Project to be null in timesheetentry!
{
_db.Entry(timesheetentry).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProjectId = new SelectList(_db.Projects, "ProjectId", "ProjectName", timesheetentry.ProjectId);
ViewBag.UserId = new SelectList(_db.Users, "UserId", "SocialSecurityNumber", timesheetentry.UserId);
return View(timesheetentry);
}
数据库:
CREATE TABLE [dbo].[TimesheetEntries] (
[UserId] INT NOT NULL,
[ProjectId] NVARCHAR (50) NOT NULL,
[EntryDate] DATE NOT NULL,
[HoursWorked] DECIMAL (8, 1) CONSTRAINT [DF_TimesheetEntries_HoursWorked] DEFAULT ((0.0)) NOT NULL,
CONSTRAINT [PK_TimesheetEntries] PRIMARY KEY CLUSTERED ([UserId] ASC, [ProjectId] ASC, [EntryDate] ASC),
CONSTRAINT [FK_TimesheetEntries_Users] FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users] ([UserId]),
CONSTRAINT [FK_TimesheetEntries_Projects] FOREIGN KEY ([ProjectId]) REFERENCES [dbo].[Projects] ([ProjectId])
);
好的,所以我可以理解为什么会发生这种情况。在您的编辑页面中,您有以下行
@Html.HiddenFor(model => model.Project)
@Html.HiddenFor(model => model.User)
但这还不足以使它能够往返于与这些子对象相关的所有数据。如果您在HTML页面上查看源代码,您应该明白我的意思。隐藏的HTML字段无法处理复杂的对象。如果您真的想往返这些对象,那么您需要将它们的所有字段输出为隐藏字段,或者创建一个模板来执行此操作。或者简单地创建一个不包含子对象的视图模型。
TimesheetEntryModel=new TimesheetEntryModel();return(Model);
你这样用吗?