2016-08-04 53 views
0

我正在創建一個反饋頁面,我希望此頁面可以轉到發送頁面,我點擊submit按鈕。我一直在嘗試這個代碼,當我點擊submit,它只是停留在同一頁上...表單不起作用留在同一頁面當我點擊提交

這是我的看法:

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post)) 
{ 
    @Html.ValidationSummary() 
    @Html.AntiForgeryToken() 

    @Html.ValidationSummary("", new {@class = "text-danger"}) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Name, new {@class = "col-md-2 control-label"}) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Name, new {@class = "form-control", placeholder = "Your Name"}) 
     </div> 
    </div> 

    @Html.ValidationSummary("", new {@class = "text-danger"}) 
    <div class="form-group"> 
     @Html.LabelFor(model => model.Email, "Email", new {@class = "control-label col-sm-2"}) 
     <div class="col-md-10"> 
      @Html.EditorFor(m => m.Email, new {htmlAttributes = new {@class = "form-control", placeholder = "Email Address"}}) 
     </div> 
    </div> 

    @Html.ValidationSummary("", new {@class = "text-danger"}) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Cell, new {@class = "col-md-2 control-label"}) 
     <div class="col-md-10"> 
      @Html.EditorFor(m => m.Cell, new {htmlAttributes = new {@class = "form-control", placeholder = "Phone Number", type = "text"}}) 
     </div> 
    </div> 

    @Html.ValidationSummary("", new {@class = "text-danger"}) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Message, new {@class = "col-md-2 control-label"}) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Message, new {@class = "form-control", placeholder = "Comments", rows = "4"}) 
     </div> 
    </div> 

    <div class="col-sm-6 col-sm-offset-3"> 
     <div class="btn-toolbar"> 
      <button class="btn-raised btn-primary btn" id="submit">Submit 
       <div class="ripple-container"></div> 
      </button> 
      <button class="btn btn-default">Cancel</button> 
     </div> 
    </div> 
} 

我的控制器:

[HttpGet] 
public ActionResult Feedback() 
{ 
    ViewBag.Message = "Your contact page."; 
    return View(); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Feedback(FeedbackViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     var item = new FeedbackViewModel() 
       { 
        Name = model.Name, 
        Email = model.Email, 
        Cell = model.Cell, 
        Message = model.Message, 
       }; 

     // TODO: Add success message to ViewBag/Data so notification will be displayed 
     return RedirectToAction("Sent"); 
    } 

    // TODO Send email in c# 
    return View(model); 
} 

我的模型!

public class FeedbackViewModel 
{ 
     [Required] 
     public string Name { get; set; } 

     [Required] 
     [EmailAddress] 
     public String Email { get; set; } 

     [MinLength(10)] 
     [StringLength(13, ErrorMessage = "Please enter a valid phone number")] 
     public string Cell { get; set; } 

     [Required] 
     [StringLength(200, ErrorMessage = "Please enter more than 20 characters and less than 200", MinimumLength = 20)] 
     public string Message { get; set; } 
} 
+1

你打控制器的POST方法(您的按鈕應該是'type =「submit」'還是'')?如果你是,那麼它因爲'ModelState'是無效的。爲什麼你有多個'@ Html.ValidationSummary()'? - 它應該是'@ Html.ValidationMessageFor(m => m.Name)'等等 –

回答

1
您使用

ModelState.IsValid,您必須使用ModelState.IsValid

 if (ModelState.IsValid) 
     { 
      var item = new FeedbackViewModel() 
      { 
       Name = model.Name, 
       Email = model.Email, 
       Cell = model.Cell, 
       Message = model.Message, 
      }; 




      //TODO: Add success message to ViewBag/Data so notification will be displayed 
      return RedirectToAction("Sent"); 

     } 


     //TODOL Send email in c# 
     return View(model); 

然後加入__ @ Html.ValidationMessageFor(..)__觀看這樣的

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post)) 
        { 
         @Html.ValidationSummary() 
         @Html.AntiForgeryToken() 

        @Html.ValidationSummary("", new {@class = "text-danger"}) 
        <div class="form-group"> 
         @Html.LabelFor(m => m.Name, new {@class = "col-md-2 control-label"}) 
         <div class="col-md-10"> 
         @Html.TextBoxFor(m => m.Name, new {@class = "form-control", placeholder = "Your Name"}) 
         @Html.ValidationMessageFor(m => m.Name) 
         </div> 
        </div> 

        @Html.ValidationSummary("", new {@class = "text-danger"}) 
        <div class="form-group"> 
         @Html.LabelFor(model => model.Email, "Email", new {@class = "control-label col-sm-2"}) 
         <div class="col-md-10"> 
          @Html.EditorFor(m => m.Email, new {htmlAttributes = new {@class = "form-control", placeholder = "Email Address"}}) 
          @Html.ValidationMessageFor(m => m.Email) 
         </div> 
        </div> 

        @Html.ValidationSummary("", new {@class = "text-danger"}) 
        <div class="form-group"> 
         @Html.LabelFor(m => m.Cell, new {@class = "col-md-2 control-label"}) 
         <div class="col-md-10"> 
          @Html.EditorFor(m => m.Cell, new {htmlAttributes = new {@class = "form-control", placeholder = "Phone Number", type = "text"}}) 
          @Html.ValidationMessageFor(m => m.Cell) 
         </div> 
        </div> 

        @Html.ValidationSummary("", new {@class = "text-danger"}) 
        <div class="form-group"> 
         @Html.LabelFor(m => m.Message, new {@class = "col-md-2 control-label"}) 
         <div class="col-md-10"> 
          @Html.TextBoxFor(m => m.Message, new {@class = "form-control", placeholder = "Comments", rows = "4"}) 
          @Html.ValidationMessageFor(m => m.Message) 
         </div> 
        </div> 
        <div class="col-sm-6 col-sm-offset-3"> 
         <div class="btn-toolbar"> 
          <input type="submit" value="Submit" class="btn-raised btn-primary btn" /> 
          <button class="btn btn-default">Cancel</button> 
          </div> 
         </div> 
        }