Search code examples
asp.netasp.net-mvcsubmit-button

MVC Submit button not doing anything


This is my first ASP MVC project - it is a simple survey that I'm starting out with only a couple questions in. My submit button on the Create page isn't working, though, and I'm not sure why...

After creating a fresh MVC project, I created a "Survey" model, which contains a SurveyID and 2 properties (to hold the answers to the 2 survey questions). I then created a new Scaffolded Controller with Views based on this Survey model. All I need is Create, so I deleted all the stuff for Details, Edit, etc. I tried setting up my views so that under Home is an Index, which is the initial page you see, and an "EndOfSurvey", which is supposed to be what you see after submitting the survey answers. On Index you hit the 'Next' button which takes you to the Survey Create page. You answer the questions and then hit the 'Done' button which submits it and takes you to EndOfSurvey. The 'Done' button on Create is not doing anything - I tried putting a break point on the first line of the Create Post and it's not even getting to that break point. Below is my code with the few things I tried.

Note that I realize I probably need to do something to tell it what value each property should have based on the radio button selected. That is a separate question. But I figure if the submit button is working it should at least be firing Create, right??

Original Create view:

 @ModelType ProSurvey.ProSurvey.Models.Survey
 @Code
     ViewData("Title") = "Create"
     Layout = "~/Views/Shared/_Layout.vbhtml"
 End Code

 <h2>Managing and Downloading from Devices</h2>

 @Using (Html.BeginForm())
     @Html.AntiForgeryToken()

     @<div class="form-horizontal">
         <h4>How often do you use the following features:</h4>
         @*<hr />*@
         @Html.ValidationSummary(True, "", New With { .class = "text-danger" })
         <div class="form-group">
             <table class="table">
                 <thead>
                     <tr>
                         <th></th>
                         <th>Never</th>
                         <th>Rarely</th>
                         <th>Sometimes</th>
                         <th>Usually</th>
                         <th>Always</th>
                     </tr>
                 </thead>
                 <tbody>
                     <tr>
                         <td>@Html.DisplayNameFor(Function(model) model.mddbccu)</td>
                         <td>
                             <div class="radio">
                                 <input name="mddbcuu" id="mddbcuu0" value="Never" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddbcuu" id="mddbcuu1" value="Rarely" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                  <input name="mddbcuu" id="mddbcuu2" value="Sometimes" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddbcuu" id="mddbcuu3" value="Usually" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddbcuu" id="mddbcuu4" value="Always" checked="" type="radio">
                             </div>
                         </td>
                     </tr>
                     <tr>
                         <td>@Html.DisplayNameFor(Function(model) model.mddtfuu)</td>
                         <td>
                             <div class="radio">
                                 <input name="mddtfuu" id="mddtfuu0" value="Never" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddtfuu" id="mddtfuu1" value="Rarely" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddtfuu" id="mddtfuu2" value="Sometimes" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                 <input name="mddtfuu" id="mddtfuu3" value="Usually" checked="" type="radio">
                             </div>
                         </td>
                         <td>
                             <div class="radio">
                                  <input name="mddtfuu" id="mddtfuu4" value="Always" checked="" type="radio">
                             </div>
                         </td>
                     </tr>
                 </tbody>
             </table>
         </div>
     </div>

 End Using

 <div class="row">
     <div class="col-md-4" style="align-content:center">
         <button class="btn btn-default">Back</button>
     </div>
     <div class="col-md-4" style="align-content:center">
         <p>Progress: []</p>
     </div>
     <div class="col-md-4" style="align-content:center">
         <input type="submit" value="Done" class="btn btn-default">
     </div>
 </div>

 @Section Scripts 
     @Scripts.Render("~/bundles/jqueryval")
 End Section

Original Survey Controller:

 Imports System
 Imports System.Collections.Generic
 Imports System.Data
 Imports System.Data.Entity
 Imports System.Linq
 Imports System.Threading.Tasks
 Imports System.Net
 Imports System.Web
 Imports System.Web.Mvc
 Imports ProSurvey.Models
 Imports ProSurvey.ProSurvey.Models

 Namespace Controllers
     Public Class SurveyController
         Inherits System.Web.Mvc.Controller

         Private db As New ProSurveyContext

         ' GET: Survey
         Async Function Index() As Task(Of ActionResult)
             Return View(Await db.Surveys.ToListAsync())
         End Function

         Public Sub New()
         End Sub

         ' GET: Survey/Create
         Function Create() As ActionResult
             Return View()
         End Function

         ' POST: Survey/Create
         'To protect from overposting attacks, please enable the specific properties you want to bind to, for 
         'more details see http://go.microsoft.com/fwlink/?LinkId=317598.
         <HttpPost()>
         <ValidateAntiForgeryToken()>
         Async Function Create(<Bind(Include:="SurveyID,mddbccu,mddtfuu")> ByVal survey As Survey) As Task(Of ActionResult)
             If ModelState.IsValid Then
                 db.Surveys.Add(survey)
                 Await db.SaveChangesAsync()
                 Return RedirectToAction("EndOfSurvey")
             End If
             Return View(survey)
         End Function

         Protected Overrides Sub Dispose(ByVal disposing As Boolean)
             If (disposing) Then
                 db.Dispose()
             End If
             MyBase.Dispose(disposing)
         End Sub
     End Class
 End Namespace

Based on a couple other SO questions I found, I tried:

Changing @Using (Html.BeginForm()) to @Using (Html.BeginForm("Create", "Survey", FormMethod.Post))

Commenting out the Scripts section in the View

Also commenting out the "If ModelState.IsValid.." and putting my breakpoint on db.Surveys.Add(survey). It still didn't get to my breakpoint.

Not sure what else to try at this point. Any ideas? Thank you very much!

Survey Model:

 Imports System.ComponentModel

 Namespace ProSurvey.Models

     Public Class Survey

         Private surveyIDInt As Integer           'Survey ID
         Private mddbccuStr As String
         Private mddtfuuStr As String

         Public Property SurveyID() As Integer
             Get
                 Return surveyIDInt
             End Get
             Set(ByVal value As Integer)
                 surveyIDInt = value
             End Set
         End Property

         Public Property mddbccu() As String
             Get
                 Return mddbccuStr
             End Get
             Set(ByVal value As String)
                 mddbccuStr = value
             End Set
         End Property

         Public Property mddtfuu() As String
             Get
                 Return mddtfuuStr
             End Get
             Set(ByVal value As String)
                 mddtfuuStr = value
             End Set
         End Property

     End Class

 End Namespace

Solution

  • The submit button should be inside the @Using of the form:

    @ModelType ProSurvey.ProSurvey.Models.Survey
     @Code
         ViewData("Title") = "Create"
         Layout = "~/Views/Shared/_Layout.vbhtml"
     End Code
    
     <h2>Managing and Downloading from Devices</h2>
    
     @Using (Html.BeginForm())
         @Html.AntiForgeryToken()
    
         @<div class="form-horizontal">
             <h4>How often do you use the following features:</h4>
             @*<hr />*@
             @Html.ValidationSummary(True, "", New With { .class = "text-danger" })
             <div class="form-group">
                 <table class="table">
                     <thead>
                         <tr>
                             <th></th>
                             <th>Never</th>
                             <th>Rarely</th>
                             <th>Sometimes</th>
                             <th>Usually</th>
                             <th>Always</th>
                         </tr>
                     </thead>
                     <tbody>
                         <tr>
                             <td>@Html.DisplayNameFor(Function(model) model.mddbccu)</td>
                             <td>
                                 <div class="radio">
                                     <input name="mddbcuu" id="mddbcuu0" value="Never" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddbcuu" id="mddbcuu1" value="Rarely" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                      <input name="mddbcuu" id="mddbcuu2" value="Sometimes" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddbcuu" id="mddbcuu3" value="Usually" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddbcuu" id="mddbcuu4" value="Always" checked="" type="radio">
                                 </div>
                             </td>
                         </tr>
                         <tr>
                             <td>@Html.DisplayNameFor(Function(model) model.mddtfuu)</td>
                             <td>
                                 <div class="radio">
                                     <input name="mddtfuu" id="mddtfuu0" value="Never" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddtfuu" id="mddtfuu1" value="Rarely" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddtfuu" id="mddtfuu2" value="Sometimes" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                     <input name="mddtfuu" id="mddtfuu3" value="Usually" checked="" type="radio">
                                 </div>
                             </td>
                             <td>
                                 <div class="radio">
                                      <input name="mddtfuu" id="mddtfuu4" value="Always" checked="" type="radio">
                                 </div>
                             </td>
                         </tr>
                     </tbody>
                 </table>
             </div>
         </div>
    
    
    
     <div class="row">
         <div class="col-md-4" style="align-content:center">
             <button class="btn btn-default">Back</button>
         </div>
         <div class="col-md-4" style="align-content:center">
             <p>Progress: []</p>
         </div>
         <div class="col-md-4" style="align-content:center">
             <input type="submit" value="Done" class="btn btn-default">
         </div>
     </div>
    End Using
    
     @Section Scripts 
         @Scripts.Render("~/bundles/jqueryval")
     End Section