私が持っているデータベースには:
PK CountryID int notnull
PK ServiceID int notnull
PK TaskID int notnull
PK TaskItemID int notnull
PK CorrespondentID int notnull
PK PreviousTask int notnull
IsOnTimeline bit not null
..and 5 other nullable fields
この複合キーを使用して、「ServiceSchedule」アイテムの一意性を確保しています。「Previoustask」フィールドは、すべてのPKを連結したものであり、レコードからレコードに関連する詳細な「ServiceSchedule」タスクのタイムラインを作成できるという考えです。
コンテキストは次のとおりです。
UPEntities db = new UPEntities(); for all workings here (i.e. repository and db).
2つのコントローラーの編集アクションは次のようになります。
public ActionResult Edit(int countryid,
int serviceid,
int taskid,
int taskitemid,
int correspondentid)
{
var t = (from s in repository.GetServiceSchedules()
where s.CountryID == countryid
where s.ServiceID == serviceid
where s.TaskID == taskid
where s.TaskItemID == taskitemid
where s.CorrespondentID == correspondentid
select s).First();
return View(t);
}
[HttpPost]
public ActionResult Edit(ServiceSchedule serviceToEdit)
{
var originalService = (from s in repository.GetServiceSchedules()
where s.CountryID == serviceToEdit.CountryID
where s.ServiceID == serviceToEdit.ServiceID
where s.TaskID == serviceToEdit.TaskID
where s.TaskItemID == serviceToEdit.TaskItemID
where s.CorrespondentID == serviceToEdit.CorrespondentID
select s).First();
if (!ModelState.IsValid)
return View(originalService);
db.ApplyPropertyChanges(originalService.EntityKey.EntitySetName, serviceToEdit); //Code throws exception here.......
db.SaveChanges();
return RedirectToAction("Index");
}
「db.ApplyPropertyChanges」行まではすべて正常に機能しているようで、エラーが発生します。
The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'ProjectName.Models.ServiceSchedule'.
編集ビューに渡されたモデルには、関連するテーブルのコレクションが含まれていることに気付きました(たとえば、CountryIDから離れた国のコレクション)が、モデルがビューから戻ると、「ServciceSchedule」テーブルのPK IDのみが含まれ、関連するものは含まれません。コレクション。
私は現在、このようなビューにhiddenforsを作成することでこの問題に対処しようとしていますが、それでも完全なモデルをコントローラーに戻すことができないようです。
<% using (Html.BeginForm())
{%>
<%= Html.ValidationSummary(true)%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.HiddenFor(model => model.CountryID)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.ServiceID)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.TaskID)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.TaskItemID)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.CorrespondentID)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.Services)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.Tasks)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.TaskItems)%>
</div>
<div class="editor-label">
<%= Html.HiddenFor(model => model.Correspondents)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.PreviousTask)%>
<%= Html.ValidationMessageFor(model => model.PreviousTask)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Cost)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Cost, String.Format("{0:F}", Model.Cost))%>
<%= Html.ValidationMessageFor(model => model.Cost)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.ChargeOut)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.ChargeOut, String.Format("{0:F}", Model.ChargeOut))%>
<%= Html.ValidationMessageFor(model => model.ChargeOut)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.DurationInDays)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.DurationInDays, String.Format("{0:F}", Model.DurationInDays))%>
<%= Html.ValidationMessageFor(model => model.DurationInDays)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.JobSection)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.JobSection)%>
<%= Html.ValidationMessageFor(model => model.JobSection)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.JobSectionGroup)%>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.JobSectionGroup)%>
<%= Html.ValidationMessageFor(model => model.JobSectionGroup)%>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
また、GetServiceSchedulesリポジトリは次のようになります。
public class ServicesRepository
{
UPEntities db = new UPEntities();
public List<ServiceSchedule> GetServiceSchedules()
{
return db.ServiceScheduleSet.Include("Countries")
.Include("Services")
.Include("Tasks")
.Include("TaskItems")
.Include("Correspondents")
.ToList();
}
}
(VS 2008、SQL 2008、MVC2を使用)
誰かが私にいくつかのより提案された救済策を教えてもらえますか?