2016-08-04 17 views
0

Ich erstelle eine Feedback-Seite, ich möchte, dass diese Seite auf die Seite gesendet wird, sobald ich auf die Schaltfläche submit klicke. Ich habe diesen Code versucht, wenn ich submit klicken, es bleibt nur auf der gleichen Seite ...Formular funktioniert nicht auf der gleichen Seite, wenn ich auf "Senden" klicke

Das ist meine Ansicht ist:

@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> 
} 

Mein Controller:

[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); 
} 

Mein Modell :

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

anzuzeigen schlagen Sie die POST-Methode (Ihre Taste des Controllers sollte sei 'type =" submit "' oder '')? Und wenn Sie sind, dann ist das, weil 'ModelState' nicht gültig ist. Und warum hast du mehrere '@ Html.ValidationSummary()'? - Es sollte '@ Html.ValidationMessageFor (m => m.Name)' usw. sein –

Antwort

1

Sie verwenden ModelState.IsValid, müssen Sie 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); 

fügen Sie dann mit __ @ Html.ValidationMessageFor (..) __ wie die

@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> 
        }