2015-05-26 66 views
0

這是我的第一個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 
+0

你在VB做的MVC?這只是傷心。 – thomasb

回答

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 
+0

哈哈... duuuuuh。謝謝!!這工作。雖然當然,當我點擊完成「調查」沒有定義鍵時,它給我一個錯誤。猜猜我現在必須檢查出來...... – Andarta

+0

我修正了'Survey'沒有定義鍵的錯誤。現在完成按鈕工作,我被帶到EndOfSurvey頁面,並將它們放入我的數據庫中。我現在遇到的問題是,第一個問題的價值不是註冊 - 只有最後一個問題。例如,我將按順序提交「從不」和「很少」 - 數據庫中的那一行包含NULL和「很少」。任何想法爲什麼?謝謝! – Andarta

+0

您可以放置​​Survey類的完整代碼嗎? – Ala