'vb.net MVC - Empty model when form is submitted
Ok so tearing my hear out as a newbie MVC dev trying to get simply forms submitted on my new MVC site. But when they submit the model shows as empty.
Simple contact us page with name, email and message fields
Here's the simple contact.vbhtml - Has a model type of:
@ModelType Page_ContactUs
form markup
@Using Html.BeginForm("Contact", "AboutUsController", FormMethod.Post, New With {.id = "contfrm"})
@Html.AntiForgeryToken()
@Html.ValidationSummary(True)
Html.EnableClientValidation()
@<div Class="form-group">
<span Class="input-icon">
<i Class="material-icons">person</i>
@Html.TextBoxFor(Function(model) model.ContactUs.Name, New With {.class = "form-control", .required = "required", .placeholder = "Enter your Full Name"})
@Html.ValidationMessageFor(Function(model) model.ContactUs.Name, "", New With {.class = "text-danger"})
</span>
</div>
@<div Class="form-group">
<span Class="input-icon">
<i Class="material-icons">mail_outline</i>
@Html.TextBoxFor(Function(model) model.ContactUs.Email, New With {.class = "form-control", .[type] = "email", .required = "required", .placeholder = "Enter your Email"})
@Html.ValidationMessageFor(Function(model) model.ContactUs.Email, "", New With {.class = "text-danger"})
</span>
</div>
@<div Class="form-group">
<span Class="input-icon">
<i Class="material-icons">comment</i>
@Html.TextAreaFor(Function(model) model.ContactUs.Enquiry, New With {.class = "form-control", .rows = "5", .required = "required", .placeholder = "Enter your Enquiry", .onchange = "RemoveSpace(this)"})
@Html.ValidationMessageFor(Function(model) model.ContactUs.Enquiry, "", New With {.class = "text-danger"})
</span>
</div>
@<Button type="submit" Class="btn btn-primary">Send</Button>
End Using
The page_contactus model looks like this:
Public Class Page_ContactUs
Public ContactUs As Contactus
Public Content As clsContent
Public Signup As Signup
End Class
My Contact Us model looks like this:
Public Class Contactus
Private _Name As String
Private _Email As String
Private _Enquiry As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Email() As String
Get
Return _Email
End Get
Set(ByVal value As String)
_Email = value
End Set
End Property
Public Property Enquiry() As String
Get
Return _Enquiry
End Get
Set(ByVal value As String)
_Enquiry = value
End Set
End Property
End Class
And my Contact us controller has this as the http post action, but when it gets here the model is empty
<HttpPost()>
<AllowAnonymous()>
<ValidateAntiForgeryToken()>
<ActionName("contact")>
Function Contact(Model As Page_ContactUs) As ActionResult
End Function
What am I doing wrong? Cant believe forms are so complicated in MVC, thought this was supposed to make things easier not harder - coming form a web forms background :-(
What have I missed please?
This is still an issue have another page with same problem AGAIN
top of the page has:
@ModelType Page_ReturnItems
form looks like this
@Using Html.BeginForm("CompleteReturn", "Account", FormMethod.Post, New With {.class = "form-style-1", .id = "retrnfrm"})
@<div>
<input type="checkbox" id="chkTerms" name="chkTerms" /> I have read, understand And agree To our <a href="/about-us/returns/" target="_blank"><b>Returns Terms & Conditions</b></a><br />
</div>
@<button type="submit" id="buttretitm" Class="btn btn-success" title="Submit Return">Create Return</button>
End Using
and it renders like this:
<form action="" class="form-style-1" id="retrnfrm" method="post"> <div>
I have read, understand And agree To our Returns Terms & Conditions
And then in the Account Controller shows this:
<HttpPost()>
<AllowAnonymous()> <ActionName("CompleteReturn")> Function CompleteReturn(Model As Page_ReturnItems) As ActionResult Try If ModelState.IsValid Then Dim PageValidated As Boolean = True Else ViewData("ErrorMessage") = "Please enter required fields and try again." End If Catch ex As Exception Logger.LogError(ex) End Try End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
