0

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を表示する必要はありませんが、コントローラーに渡されるパラメーターとユーザーが選択した残りの情報を使用して、新しいオブジェクトを作成する必要があります。

4

1 に答える 1

1

方法 1 : customerId を表示する場合は、次のコードを使用できます...

<div class="editor-label">
 @Html.LabelFor(Function(model) model.CustomerId)
</div>

このようにして、CustomerId が表示され、ユーザーはそれを変更できず、ポストバックが完了しても、この値はモデルの一部として引き続き存在します。

方法 2 :表示したくないが、それでも上記の目的で使用する場合は、以下に記述されたコードを使用します...

<div class="editor-label">
 @Html.HiddenFor(Function(model) model.CustomerId)
</div>

これがあなたが望んでいたものであることを願っています。

于 2012-08-13T11:48:10.377 に答える