We help IT Professionals succeed at work.
Get Started

ASP.NET MVC: Model in strongly typed view not populating

103 Views
Last Modified: 2020-02-14
I am brand spanking new to ASP.NET MVC and I'm working through a tutorial to get the basics. The tutorial is in C# and I'm converting it to VB to stay consistent with some related projects. I have to assume that I'm missing something in that conversion but can't figure out what it could be.

I have the following class:

Public Class GuestResponse
    Public Name As String
    Public Email As String
    Public Phone As String
    Public WillAttend As Boolean?
End Class

Open in new window


and a strongly-typed view related to that class:

@ModelType PartyInvites.GuestResponse

@Code
    Layout = Nothing
End Code

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RsvpForm</title>
</head>
<body>
    @Using Html.BeginForm
        @<p>Your name: @Html.TextBoxFor(Of String)(Function(x) x.Name)</p>
        @<p>Your email: @Html.TextBoxFor(Of String)(Function(x) x.Email)</p>
        @<p>Your phone: @Html.TextBoxFor(Of String)(Function(x) x.Phone)</p>
        @<p>
            Will you attend?
            @Html.DropDownListFor(Of Boolean?)(Function(x) x.WillAttend,
                                                        New SelectListItem() {New SelectListItem With {.Text = "Yes, I'll be there", .Value = Boolean.TrueString},
                                                        New SelectListItem With {.Text = "No, I can't come", .Value = Boolean.FalseString}}, "Choose an option")
        </p>
        @<input type="submit" value="Submit RSVP" />
    End Using
</body>
</html>

Open in new window


My controller has a simple action method that takes that class and responds with another view:

<HttpPost>
Function RsvpForm(g As GuestResponse) As ViewResult
    'TODO: Email response to the party organizer
    Return View("Thanks", g)
End Function

Open in new window


Unfortunately, the form data from the post request isn't being populated in my GuestResponse class. If I insert a breakpoint in that action method, all the GuestResponse properties are empty. At the same time, the Request.Form values look as expected - they're just not filling in the GuestResponse object.

I'm sure it's something small I'm missing. Any help is appreciated.
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE