【发布时间】:2015-05-26 14:09:36
【问题描述】:
这是我的第一个 ASP MVC 项目 - 这是一个简单的调查,我刚开始时只有几个问题。但是,我在“创建”页面上的提交按钮不起作用,我不知道为什么...
创建一个新的 MVC 项目后,我创建了一个“调查”模型,其中包含一个 SurveyID 和 2 个属性(用于保存 2 个调查问题的答案)。然后,我基于此调查模型创建了一个带有视图的新脚手架控制器。我只需要创建,所以我删除了详细信息、编辑等的所有内容。我尝试设置我的视图,以便在主页下有一个索引,这是你看到的初始页面,以及一个“EndOfSurvey”,应该是成为您提交调查答案后所看到的。在索引上,您点击“下一步”按钮,将您带到调查创建页面。您回答问题,然后点击“完成”按钮提交问题并将您带到 EndOfSurvey。 Create 上的“完成”按钮没有做任何事情 - 我尝试在 Create Post 的第一行设置一个断点,但它甚至没有到达那个断点。下面是我尝试过的几件事的代码。
请注意,我意识到我可能需要根据所选单选按钮告诉它每个属性应该具有什么值。那是一个单独的问题。但我认为如果提交按钮正在工作,它至少应该触发 Create,对吧??
原始创建视图:
@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(survey) 上。它仍然没有到达我的断点。
目前不确定还可以尝试什么。有任何想法吗?非常感谢!
调查模型:
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
【问题讨论】:
-
你在 VB 中做 MVC 吗?这只是悲伤。
标签: asp.net asp.net-mvc submit-button