我对 MVC 很陌生,在提交表单和让控制器获取发布的值时遇到了一些麻烦。
似乎正在发生的事情是,虽然表单确实发布到控制器中的正确方法,但传递的模型充满了空值 - 好像它没有被表单填充。
我尝试以与默认登录控件相同的方式创建它,但我显然在某处遗漏了一些东西。任何人都可以解释一下吗?
我的代码如下:
模型
Public Class ContactUsDetails
Private _name As String
Private _email As String
Private _details As String
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Email() As String
Get
Return _email
End Get
End Property
Public ReadOnly Property Details() As String
Get
Return _details
End Get
End Property
Public Sub New(ByVal name As String, ByVal email As String, ByVal details As String)
_name = name
_email = email
_details = details
End Sub
Public Sub New
End Sub
End Class
看法
@ModelType TestMVC.ContactUsDetails
@Code
ViewData("Title") = "ContactUs"
End Code
@Using Html.BeginForm()
@<fieldset>
<legend>Contact Us</legend>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Name)
</div>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(Function(m) m.Details)
</div>
<div class="editor-field">
@Html.TextBoxFor(Function(m) m.Details)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
End Using
控制器
Namespace TestMVC
Public Class FormsController
Inherits System.Web.Mvc.Controller
'
' GET: /Forms
Public Function ContactUs() As ActionResult
Return View()
End Function
<HttpPost()> _
Public Function ContactUs(model As ContactUsDetails) As ActionResult
If ModelState.IsValid Then
End If
Return View(model)
End Function
End Class
End Namespace