2016-04-17 70 views
1

我想创建一个简单的MVC应用程序,以使用AJAX Post方法将数据插入数据库,并获取未处理的异常错误。我将代码构造成与教程页面相同,所以我很难找出错误来自哪里。不知道我是否可能需要修改sql连接。任何帮助表示赞赏。谢谢!ASP.NET MVC jQuery发布错误

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

我认为你没有在适当位置的AddEmployee视图。它应该放在'Views \ Home'文件夹中。 – thisisbobbs

+0

工作完美!它似乎没有更新我的数据库一旦'保存'btn被点击,所以现在正在调试。这可能还需要更多的连接字符串。谢谢!! – AndrewC10

回答

2

如果你正在做一个帖子期待POST的结果返回,返回JSON而不是查看

return Json(new {Success = true}); 

或任何状态要回来。如果您返回浏览器的网络视图,则返回视图效果会很好。

请记住,如果你想用GET返回JSON数据,您需要构建您的return语句不同

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

谢谢。我只是在学习Post的基础知识,但是无法获得提交给数据库的数据。我相信这是我正在使用的连接字符串。再次感谢。 – AndrewC10

2

我想你没有在正确的位置AddEmployee视图。它应该放在Views\Home文件夹中。

请确保您更新您的行动@Anthony建议。

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

谢谢。刚做了更新,我仍然在学习返回View vs Json的区别。感谢你对此的帮助,我显然是初学者。 – AndrewC10

0

至于你的信息

http://localhost:99999/Home/AddEmployee 

是一个简单的HTTP GET请求但你装饰你的方法

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

      return View(); 
     } 

HTTP POSTEmployee object.So有一个错误。

编辑您的帖子应该返回这样

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

      return Json(obj); 
     } 

相反的ActionResult的。

+0

请看看这个错误,它与view不是HttpMethods有关。 – thisisbobbs