たとえば、ユーザークラスとユーザーワークの子クラス(エンティティフレームワークのスキャフォールディングクラス)を使用した親子の状況があります。コントローラー ウィザードを使用して、CRUD メソッドとビューをスキャフォールディングしました。2 つの Create メソッド、Get メソッドと httpPost メソッドを足場にしました。
通常のシナリオでは、ユーザーの ID を get に渡し、html.beginform() セクションに入力されているモデルに渡され、その後、post に渡されるようにしたいと考えています。
これを行う正しい方法は何ですか?Get を以下のように変更しました
public ActionResult Create(int id)
{
this.ViewData.Add("id", id);
return View();
}
および以下のように Create.shtml
@model UserWork
@{
ViewBag.Title = "Create";
int id = (int)ViewData["id"];
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
@using (Html.BeginForm(routeValues:new {user_id = @id})) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Userwork</legend>
<div class="editor-label">
@Html.LabelFor(model => model.user_id)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.user_id)
@Html.ValidationMessageFor(model => model.user_id)
</div>
......
(フォームがロードされたときに事前入力されていることをテストできるように、スキャフォールディングされたuser_idフィールドを編集可能なままにしました)フォームがロードされ、フィールドにID値がありません。これはこれを達成する正しい方法ですか?
乾杯ティム