已添加具有相同密钥的项目。
这就是我尝试提交时遇到的错误,我知道这是因为我在 workhoursmodel 中有两行同名,但是我该如何修复它 - 有什么简单的方法可以修复还是我必须崩溃表并重新创建它?
ps 我对 mvc 真的很陌生,所以有点帮助会很好 ds
我的控制器
public ActionResult Create()
{
WorkhoursModels model = new WorkhoursModels();
model.Workstations = db.WorkStation.AsEnumerable().Select(x => new SelectListItem() { Text = x.WorkStations, Value = x.Id.ToString() });
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(WorkhoursModels workhoursmodels)
{
db.WorkHours.Add(workhoursmodels);
db.SaveChanges();
return View();
}
{
我的模型
{
[Table("WorkHoursModels")]
public class WorkhoursModels
{
public int Id { get; set; }
public string Start { get; set; }
public string Stop { get; set; }
public string command { get; set; }
public string Users { get; set; }
public IEnumerable<System.Web.Mvc.SelectListItem> Workstations { get; set; }
public string WorkStations { set; get; }
}
[Table("WorkStations")]
public class WorkHoursStation
{
public int Id { get; set; }
public string WorkStations { get; set; }
public IEnumerable<SelectListItem> Workstation { get; set; }
}
public class WorkhourDB : DbContext
{
public DbSet<WorkhoursModels> WorkHours { get; set; }
public DbSet<WorkHoursStation> WorkStation { get; set; }
}
}
我的观点
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Users)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Users)
@Html.ValidationMessageFor(model => model.Users)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Id, Model.Workstations, string.Empty)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
用数据库信息更新了问题
CREATE TABLE [dbo].[WorkhoursModels] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Start] NVARCHAR (50) NULL,
[Stop] NVARCHAR (50) NULL,
[command] NVARCHAR (50) NULL,
[users] NVARCHAR (MAX) NULL,
[WorkStations] NVARCHAR (50) NULL,
CONSTRAINT [PK_dbo.WorkhoursModels] PRIMARY KEY NONCLUSTERED ([Id] ASC)
);