这是我的第一个ASP MVC项目 - 这是一个简单的调查,我刚开始时只有几个问题。然而,创建页面上的我的提交按钮不起作用,并且我不确定为什么......MVC提交按钮不做任何事
创建一个新的MVC项目后,我创建了一个“Survey”模型,其中包含一个SurveyID和2个属性(用于保存2个调查问题的答案)。然后,我根据此Survey模型创建了一个带有Views的新Scaffolded Controller。所有我需要的是创建,所以我删除了所有的东西的详细信息,编辑等。我试图设置我的意见,以便在Home下是一个索引,这是你看到的初始页面,和一个“EndOfSurvey”,这是应该的就是你在提交调查答案后看到的结果。在索引上,点击'下一步'按钮,您可以进入调查创建页面。您回答问题,然后点击提交的“完成”按钮,并将您带到EndOfSurvey。 “创建”上的“完成”按钮没有做任何事情 - 我尝试在创建贴的第一行放置一个断点,它甚至没有进入该断点。下面是我的代码,我尝试了几件事情。
请注意,我意识到我可能需要做一些事情来根据所选的单选按钮来告诉它每个属性应具有的值。这是一个单独的问题。但我认为,如果提交按钮正在工作,它应该至少被射击创建,对吗?
原始创建视图:
@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(“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?这只是伤心。 – thomasb