【问题标题】:Can't Create Contact Form in MVC 3无法在 MVC 3 中创建联系表单
【发布时间】:2012-10-02 14:07:02
【问题描述】:

我正在尝试创建一个简单的联系表单。这是我的模型:

Imports System.Data.Entity
Imports System.ComponentModel.DataAnnotations

Public Class Contact

    Public Property Name() As String
    Public Property Title() As String
    Public Property Company() As String
    Public Property CompanyAddress() As String
    Public Property PhoneNumber() As String
    Public Property NumberOfEmployees() As String
    Public Property EmailAddress() As String
    Public Property Subject() As String
    Public Property Message() As String

End Class

这是我的观点:

@ModelType MyBlog.Contact
@Code
    ViewData("Title") = ViewBag.Title
End Code
@Using Html.BeginForm()

    @Html.LabelFor(Function(model) model.EmailAddress)
    @Html.EditorFor(Function(model) model.EmailAddress)
    @Html.ValidationMessageFor(Function(model) model.EmailAddress)

    @<input type="submit" value="Send" />

End Using

还有,这是我的控制器:

Function Contact() As ActionResult

    Return View("", "_FinalSubPageLayout", "")

End Function

我得到的错误是:

传入字典的模型项是'System.String'类型, 但是这本词典需要一个“MyBlog.Contact”类型的模型项。

我可以做些什么来解决这个错误?谢谢。

【问题讨论】:

    标签: vb.net asp.net-mvc-3 razor


    【解决方案1】:
     'Return View("", "_FinalSubPageLayout", "")
    
      Dim myModel as New Blog.Contact
      'fill myModel
    
      Return View("", "_FinalSubPageLayout", myModel)
    

    我不确定第一个 "",您可能需要指定实际名称或传递 Nothing 或其他内容。

    【讨论】:

    • 谢谢,但它不是变量。这是一个布局名称_FinalSubPageLayout.vbhtml。您可以将其替换为View(),这将呈现具有相同错误的视图(只是不在布局内)。
    • 真的很好,伙计们。我没有意识到我必须将模型发送到那里。
    【解决方案2】:

    您将"" 作为模型传递给您的视图,该视图需要MyBlog.Contact 类型的模型。我不确定这两个 "" 参数中的哪一个应该是模型,但您需要将其设为 MyBlog.Contact 的实例或使用不需要模型的重载。

    【讨论】:

    • 真的很好,伙计们。我没有意识到我必须将模型发送到那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2015-03-07
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多