MVC提交按钮没有做任何事

时间:2015-05-26 14:09:37

标签: asp.net asp.net-mvc submit-button

这是我的第一个ASP MVC项目 - 这是一个简单的调查,我开始只有几个问题。但是,我在“创建”页面上的提交按钮不起作用,我不知道为什么...

创建一个新的MVC项目后,我创建了一个“Survey”模型,其中包含SurveyID和2个属性(用于保存2个调查问题的答案)。然后,我基于此Survey模型创建了一个带有Views的新Scaffolded Controller。我只需要创建,所以我删除了所有的细节,编辑等等。我尝试设置我的视图,以便在Home下面是一个索引,这是你看到的初始页面,以及一个“EndOfSurvey”,这是假设的在提交调查答案后,你会看到什么。在索引上,您可以点击“下一步”按钮,进入“调查创建”页面。您回答问题,然后点击提交它的“完成”按钮,然后转到EndOfSurvey。 Create上的'Done'按钮没有做任何事情 - 我尝试在Create Post的第一行设置一个断点,它甚至没有达到那个断点。以下是我尝试的一些事情的代码。

请注意,我意识到我可能需要根据所选的单选按钮告诉它每个属性应具有的值。这是一个单独的问题。但我认为如果提交按钮正常工作,它至少应该触发创建,对吧?

原始创建视图:

 @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

原始调查控制器:

 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

基于我发现的其他几个问题,我试过了:

将@Using(Html.BeginForm())更改为@Using(Html.BeginForm(“创建”,“调查”,FormMethod.Post))

在视图中注释掉“脚本”部分

同时注释掉“If ModelState.IsValid ..”并将我的断点放在db.Surveys.Add(调查)上。它仍然没有达到我的断点。

目前还不确定还有什么可以尝试的。有任何想法吗?非常感谢你!

调查模型:

 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

1 个答案:

答案 0 :(得分:3)

提交按钮应位于表单的@Using中:

@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