MVC 提交按钮没有做任何事情
MVC Submit button not doing anything
这是我的第一个 ASP MVC 项目 - 这是一个简单的调查,我刚开始时只问了几个问题。不过,我在“创建”页面上的提交按钮不起作用,并且我不确定为什么...
创建一个新的 MVC 项目后,我创建了一个 "Survey" 模型,其中包含一个 SurveyID 和 2 个属性(用于保存 2 个调查问题的答案)。然后我基于这个 Survey 模型创建了一个带有视图的新脚手架控制器。我只需要创建,所以我删除了所有详细信息、编辑等内容。我尝试设置我的视图,以便在主页下有一个索引,这是您看到的初始页面,还有一个 "EndOfSurvey",它应该是您提交调查答案后所看到的。在 Index 上,您点击 'Next' 按钮,这会将您带到 Survey Create 页面。您回答问题,然后点击 'Done' 按钮提交它并带您到 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
根据我发现的其他几个 SO 问题,我尝试了:
将@Using (Html.BeginForm()) 更改为@Using (Html.BeginForm("Create", "Survey", 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
提交按钮应该在表单的@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
这是我的第一个 ASP MVC 项目 - 这是一个简单的调查,我刚开始时只问了几个问题。不过,我在“创建”页面上的提交按钮不起作用,并且我不确定为什么...
创建一个新的 MVC 项目后,我创建了一个 "Survey" 模型,其中包含一个 SurveyID 和 2 个属性(用于保存 2 个调查问题的答案)。然后我基于这个 Survey 模型创建了一个带有视图的新脚手架控制器。我只需要创建,所以我删除了所有详细信息、编辑等内容。我尝试设置我的视图,以便在主页下有一个索引,这是您看到的初始页面,还有一个 "EndOfSurvey",它应该是您提交调查答案后所看到的。在 Index 上,您点击 'Next' 按钮,这会将您带到 Survey Create 页面。您回答问题,然后点击 'Done' 按钮提交它并带您到 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
根据我发现的其他几个 SO 问题,我尝试了:
将@Using (Html.BeginForm()) 更改为@Using (Html.BeginForm("Create", "Survey", 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
提交按钮应该在表单的@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