0
我想爲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,但需要與傳遞給控制器的參數和信息,用戶的其餘部分創建新對象選擇。