我正在尝试为 MVC 为我创建的脚手架创建视图设置默认值。我尝试创建模型的一个实例并将其传递给视图,但它会抛出错误。
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,但需要使用传递给控制器的参数和用户选择的其余信息来创建新对象。