2016-04-17 9 views
1

Ich versuche, eine einfache MVC App zu erstellen, um Daten in eine Datenbank mit AJAX Post-Methode einfügen und bekomme einen unbehandelten Ausnahmefehler. Ich habe den Code so strukturiert, dass er mit der Tutorial-Seite identisch ist. Daher habe ich Probleme, herauszufinden, woher der Fehler kommt. Nicht sicher, ob ich möglicherweise die SQL-Verbindung ändern muss. Jede Hilfe wird geschätzt. Vielen Dank!ASP.NET MVC jQuery Beitrag Fehler

Model: 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MvcAjax.Models 
{ 
    public class Employee 
    { 
     public string Name { get; set; } 
     public string City { get; set; } 
     public string Address { get; set; } 
    } 
} 

View: 
@{ 
    ViewBag.Title = "AddEmployee"; 
} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script> 

    $(document).ready(function() { 
//function will be called on button click having id btnsave 
     $("#btnSave").click(function() { 
      $.ajax(
      { 
       type: "POST", //HTTP POST Method 
       url: "Home/AddEmployee", // Controller/View 
       data: { //Passing data 
        Name: $("#txtName").val(), //Reading text box values using Jquery 
        City: $("#txtAddress").val(), 
        Address: $("#txtcity").val() 
       } 

      }); 

     }); 
    }); 

</script> 
<br /><br /> 
<fieldset> 
    <div class="form-horizontal"> 
     <div class="editor-label"> 
      Name 
     </div> 
     <div class="editor-label"> 
      <input type="text" id="txtName" /> 
     </div> 

     <div class="editor-label"> 
      Address 
     </div> 
     <div class="editor-label"> 
      <input type="text" id="txtAddress" /> 
     </div> 

     <div class="editor-label"> 
      City 
     </div> 
     <div class="editor-label"> 
      <input type="text" id="txtcity" /> 
     </div> 
     <div class="editor-label"> 
      <br /> 
      <input class="btn-default" type="button" id="btnSave" value="Save" /> 
     </div> 
    </div> 
</fieldset> 

Controller: 
using MvcAjax.Models; 
using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace MvcAjax.Controllers 
{ 
    public class HomeController : Controller 
    { 
     private SqlConnection con; 

     // GET: AddEmployee 
     public ActionResult AddEmployee() 
     { 

      return View(); 
     } 
     //Post method to add details  
     [HttpPost] 
     public ActionResult AddEmployee(Employee obj) 
     { 
      AddDetails(obj); 

      return View(); 
     } 

     //To Handle connection related activities  
     private void connection() 
     { 
      string constr = ConfigurationManager.ConnectionStrings["SqlConn"].ToString(); 
      con = new SqlConnection(constr); 

     } 
     private void AddDetails(Employee obj) 
     { 
      connection(); 
      SqlCommand com = new SqlCommand("AddEmp", con); 
      com.CommandType = CommandType.StoredProcedure; 
      com.Parameters.AddWithValue("@Name", obj.Name); 
      com.Parameters.AddWithValue("@City", obj.City); 
      com.Parameters.AddWithValue("@Address", obj.Address); 
      con.Open(); 
      com.ExecuteNonQuery(); 
      con.Close(); 

     } 
    } 

} 

Error: 
http://localhost:99999/Home/AddEmployee 


The view 'AddEmployee' or its master was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Home/AddEmployee.aspx 
~/Views/Home/AddEmployee.ascx 
~/Views/Shared/AddEmployee.aspx 
~/Views/Shared/AddEmployee.ascx 
~/Views/Home/AddEmployee.cshtml 
~/Views/Home/AddEmployee.vbhtml 
~/Views/Shared/AddEmployee.cshtml 
~/Views/Shared/AddEmployee.vbhtml 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The view 'AddEmployee' or its master was not found or no view engine supports the searched locations. The following locations were searched: 
~/Views/Home/AddEmployee.aspx 
~/Views/Home/AddEmployee.ascx 
~/Views/Shared/AddEmployee.aspx 
~/Views/Shared/AddEmployee.ascx 
~/Views/Home/AddEmployee.cshtml 
~/Views/Home/AddEmployee.vbhtml 
~/Views/Shared/AddEmployee.cshtml 
~/Views/Shared/AddEmployee.vbhtml 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
+1

Ich denke, Sie haben keine AddEmployee Ansicht an der richtigen Stelle. Es sollte im Ordner "Views \ Home" abgelegt werden. – thisisbobbs

+0

Arbeitete perfekt! Es scheint nicht, meine Datenbank zu aktualisieren, sobald 'Save' btn angeklickt wird, also arbeite ich jetzt an dem Debuggen. Das hat wahrscheinlich mehr mit meiner Verbindungszeichenfolge zu tun. Vielen Dank!! – AndrewC10

Antwort

2

Wenn Sie einen Beitrag tun, die Ergebnisse der POST erwartet zurück, kehren Json eher als eine Ansicht

return Json(new {Success = true}); 

oder was auch immer Status Sie zurückkehren möchten. Das Zurückgeben einer Ansicht funktioniert gut, wenn Sie eine Webansicht für einen Browser zurückgeben.

Denken Sie daran, wenn Sie JSON-Daten mit einem GET zurückkehren möchten, müssen Sie Ihre return-Anweisung anders

return Json(new {Success = true}, JsonRequestBehavior.AllowGet); 
+0

Danke. Ich lerne nur die Grundlagen der Post, habe aber Probleme, die Daten an die db gesendet zu bekommen. Ich glaube, das ist mehr die Verbindungszeichenfolge, mit der ich arbeite. Danke noch einmal. – AndrewC10

2

Ich glaube, Sie nicht AddEmployee Sicht in die richtige Position haben Sie konstruieren. Es sollte in Views\Home Ordner platziert werden.

Bitte stellen Sie sicher, dass Sie Ihre Aktion aktualisieren, wie von @Anthony vorgeschlagen.

return Json(new {Success = true}); 
+0

Danke. Habe gerade das Update gemacht und lerne immer noch die Unterschiede bei der Rückgabe eines Views gegenüber einem Json. Danke für die Hilfe dazu, ich bin eindeutig ein Anfänger. – AndrewC10

0

Was Ihre Informationen

http://localhost:99999/Home/AddEmployee 

ist einfach eine HTTP GET Anfrage aber Sie Ihre Methode dekorieren, wie

//Post method to add details  
     [HttpPost] 
     public ActionResult AddEmployee(Employee obj) 
     { 
      AddDetails(obj); 

      return View(); 
     } 

HTTP POST mit Employee object.So gibt es ein Fehler.

EDIT Ihr Beitrag sollte wie Rückkehr dieser

[HttpPost] 
     public JsonResult AddEmployee(Employee obj) 
     { 
      AddDetails(obj); 

      return Json(obj); 
     } 

Statt Action.

+0

Bitte schauen Sie sich den Fehler an, er bezieht sich nicht auf HttpMethods. – thisisbobbs