2012-08-13 51 views
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,但需要與傳遞給控制器​​的參數和信息,用戶的其餘部分創建新對象選擇。

回答

1

方法1:如果你想顯示在客戶,那麼你可以使用下面的代碼...

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

這樣一來,客戶ID會顯示出來,用戶不能更改它,當回傳完成後,該值仍作爲模型的一部分存在。

方法2:如果你不想顯示,但還是將其用於上述目的,然後使用下面寫的代碼...

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

希望這是你想要的。

相關問題