所以即時通訊設法啓用項目的下拉列表啓用取決於如果用戶有一個項目分配給它。不過,我不斷收到錯誤,你的標題看...即時通訊使用ASP.NET MVC 5實體框架傳遞的主鍵值數量必須與實體上定義的主鍵值數量匹配。 ASP.NET MVC實體框架
下面是一些代碼:
控制器:
namespace ReportSystem.Controllers
{
[AuthenticateRoles]
public class TimeReportsController : Controller
{
private readonly ReportsDatabaseEntities _db = new ReportsDatabaseEntities(); //Database Connection
[HttpPost]
public ActionResult Login(LoginModel model)
{
var user = _db.Consultants.FirstOrDefault(i => i.Name == model.Name);
if (user != null)
{
if (user.Password == model.Password)
{
Session["Name"] = user.Name;
Session["RoleID"] = user.RoleID;
Session["Id"] = user.Id;
return RedirectToAction("Index", "Home");
}
}
return View("Login");
}
public ActionResult Index()
{
var consultantId = (int)Session["Id"];
var timereports = _db.TimeReports.Where(c => c.ConsultantID == consultantId).ToList();
return View(timereports);
}
public ActionResult Create()
{
var projectMembers = _db.ProjectMembers.Find();
var projectId = _db.ProjectMembers.First();
if (projectMembers.ConsultantID == (int)Session["Id"])
{
if (projectId == null)
{
ViewBag.ProjectID = null;
}
}
ViewBag.ProjectID = new SelectList(_db.Project, "Id", "Name");
ViewBag.ConsultantID = new SelectList(_db.Consultants, "Id", "Name");
return View();
}
查看:
@model ReportSystem.Models.TimeReports
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.ConsultantID)
<fieldset>
<legend>TimeReports</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectID, "Project")
</div>
<div class="editor-field">
@Html.DropDownList("ProjectID", String.Empty)
@Html.ValidationMessageFor(model => model.ProjectID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TotalHours)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TotalHours)
@Html.ValidationMessageFor(model => model.TotalHours)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TimeType)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TimeType)
@Html.ValidationMessageFor(model => model.TimeType)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OnSite)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OnSite)
@Html.ValidationMessageFor(model => model.OnSite)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
SQL數據庫:
CREATE TABLE [dbo].[ProjectMembers] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[ProjectID] INT NOT NULL,
[ConsultantID] INT NOT NULL,
CONSTRAINT [PK_ProjectMembers] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_ProjectMembers_ProjectID] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Project] ([Id]),
CONSTRAINT [FK_ProjectMembers_ConsultantID] FOREIGN KEY ([ConsultantID]) REFERENCES [dbo].[Consultants] ([Id])
);
CREATE TABLE [dbo].[Consultants] (
[Id] INT NOT NULL,
[RoleID] INT NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[PayRoll] DECIMAL (9) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_Consultants] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Consultants_RoleID] FOREIGN KEY ([RoleID]) REFERENCES [dbo].[Role] ([Id])
);
CREATE TABLE [dbo].[Project] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[ProjectId] NVARCHAR (50) NOT NULL,
[Image] IMAGE NULL,
[SubProject] NVARCHAR (50) NULL,
[Role] NVARCHAR (50) NOT NULL,
[TotalHours] DECIMAL (9, 1) NOT NULL,
[ProjectType] NVARCHAR (50) NULL,
[Budget] MONEY NOT NULL,
[Orders] NVARCHAR (50) NOT NULL,
[ProfitMargin] NVARCHAR (50) NOT NULL,
[Active] BIT NOT NULL,
[ProjectStart] DATE NOT NULL,
[ProjectEnd] DATE NULL,
[TimeType] NVARCHAR (50) NOT NULL,
[KickBack] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED ([Id] ASC)
);
檢查Entity Framework如何生成您的實體,確保主鍵和外鍵按您期待的方式構建。此外,它看起來像你的ProjectMembers表有一個外鍵項目,但你沒有在你的文章中包含該表,是在實體框架?如果沒有,請將其包含在實體框架中,或者如果您沒有使用實體框架,請將該鏈接從實體中刪除。 – Paritosh 2015-03-13 13:06:49