MVC が作成した scaffold create ビューのデフォルト値を設定しようとしています。モデルのインスタンスを作成してビューに渡そうとしましたが、エラーがスローされました。
Function Create(id As Integer) As ViewResult
Dim job As Job
job.CustomerId = id
Return View(job)
End Function
次のビューを使用して、作成ビューにパラメーターの id を事前入力して、新しいジョブが作成されると、その顧客 ID で自動的に作成されるようにするにはどうすればよいですか。私の見解は以下の通りです:
@ModelType Laundry.Job
@Code
ViewData("Title") = "Create"
End Code
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
<legend>Job</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.JobDate)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.JobDate)
@Html.ValidationMessageFor(Function(model) model.JobDate)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.CustomerId)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.CustomerId)
@Html.ValidationMessageFor(Function(model) model.CustomerId)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.BillId)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.BillId)
@Html.ValidationMessageFor(Function(model) model.BillId)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.JobStatus)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.JobStatus)
@Html.ValidationMessageFor(Function(model) model.JobStatus)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.JobAmount)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.JobAmount)
@Html.ValidationMessageFor(Function(model) model.JobAmount)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
End Using
<div>
@Html.ActionLink("Back to List", "Index")
</div>
必ずしもフォームに顧客IDを表示する必要はありませんが、コントローラーに渡されるパラメーターとユーザーが選択した残りの情報を使用して、新しいオブジェクトを作成する必要があります。