ASP.NET MVC アプリケーション内で Linq to SQL を使用しています。このモデルは、Options テーブルへの外部キー リンクを含む Decisions テーブルで構成されます (1 つの決定、多くのオプション)。したがって、Model を使用すると、Decision.Options プロパティを介して Options コレクションにアクセスできます。ユーザーが他の Decision プロパティの更新とは別のビューで Decision の Options コレクションを更新できるようにしたいと考えています。Decision オブジェクトを更新するために、Options コレクションをコントローラーに戻す最良の方法は何ですか? コードは次のとおりです。
コントローラーは、決定モデルを Options ビューに送信します。
//
// GET /Decisions/Options/5
[Authorize]
public ActionResult Options(int id)
{
Decision decision = decisionRepository.GetDecision(id);
return View(decision);
}
私のビューページには、ユーザーが変更して送信するためのオプションの名前フィールドがリストされているだけです:
<% using (Html.BeginForm())
{%>
<div id="answers">
<table>
<% int i = 0; %>
<% foreach (var item in Model.Options)
{ i += 1;
%>
<tr>
<td>
<label>Option #<%= i%>: </label>
<%= Html.TextBox("Option.Name", item.Name)%>
</td>
</tr>
<% } %>
</table>
<input type="submit" name="button" value="Back" />
<input type="submit" name="button" value="Next" />
</div>
<% } %>
Options POST アクションのコントローラー コードは次のとおりです。
// POST /Decisions/Options/4
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Options(int id, string button, Decision d)
{
Decision decision = decisionRepository.GetDecision(id);
if (decision == null)
return View("NotFound");
if (button == "Back")
{
return RedirectToAction("Edit", new { id = decision.DecisionID });
}
//Update the options in the decision model
//TODO: What's the best way to update the Decision.Options parameter?
//Save options and move on to factors
UpdateModel(decision);
decisionRepository.Save();
return RedirectToAction("Factors", new { id = decision.DecisionID });
}
オプションの POST アクションで、オプション項目のコレクションを返す最良の方法は何ですか?
Save メソッドを呼び出す前に、FormCollection を直接操作して、Decision.Options に割り当てるために EntitySet に変換しようとしました。上記のコードでは、Decision オブジェクトが渡されていますが、Options コレクションは常に null であるため、TODO をコメントしました。
これで Decision オブジェクトを直接編集できるようになりましたが、Model Binder または FormCollection を使用してモデル内の子テーブルを更新する際に問題が発生しています。どんな助けでも大歓迎です!