2013-09-29 50 views
0

我正在用MVC4和VBNet使用實體框架編寫應用程序。我有兩個型號,Printer.vb和Request.vb如何創建自定義編輯視圖MVC?

他們是如下圖所示,

Request.vb

Public Class Request 
     Public Property ID() As Integer 
     Public Property User() As String 
     Public Property Printer() As Printer 
     Public Property Approval() As Boolean 
    End Class 

    Public Class RequestDBContext 
     Inherits DbContext 
     Public Property Requests() As DbSet(Of Request) 
    End Class 

Printer.vb

Public Class Printer 
     Public Property ID() As Integer 
     Public Property Name() As String 
    End Class 

    Public Class PrinterDBContext 
     Inherits DbContext 
     Public Property Printers() As DbSet(Of Printer) 
    End Class 

我有創建引用數據庫中存在的打印機模型的請求時遇到問題。

我的要求視圖看起來像這樣,

<div class="editor-field"> 
     @Html.EditorFor(Function(model) model.User) 
     @Html.EditorFor(Function(model) model.Approval) 
     @Html.EditorFor(Function(model) model.Printer) 
     @Html.ValidationMessageFor(Function(model) model.User) 
    </div> 

但是`Model.EditorFor(model.Printer)不會創建任何東西。

如何向請求視圖添加一個字段,該字段將查找打印機並在該請求中引用它?

+0

可以爲打印機添加編輯模板 –

回答

1

對於MVC框架一無所知的類型,您不能只使用@Html.EditorFor()

該框架已經知道如何處理@Html.EditorFor()常見的基本類型,如字符串和int。

你需要做的就是按照教程這樣 blog post,或者你可以谷歌「自定義編輯模板Asp.net MVC」

基本上你要創建的視圖文件/共享/ EditorTemplates/Printer.vbhtml並告訴MVC框架顯示什麼。

在Printer.vbhtml它看起來像

@model Printer 

    @Html.Textbox("", Model.ID.GetValueOrDefault()) 
    @Html.Textbox("", Model.Name.GetValueOrDefault())