ASP.NET MVC jQuery发布错误

ASP.NET MVC jQuery发布错误,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,我正在尝试创建一个简单的MVC应用程序,使用AJAXPOST方法将数据插入数据库,但遇到了一个未处理的异常错误。我构造了与教程页面相同的代码,因此我很难找出错误的来源。不确定是否需要修改sql连接。感谢您的帮助。谢谢 Model: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcAjax.Models { public class Empl

我正在尝试创建一个简单的MVC应用程序,使用AJAXPOST方法将数据插入数据库,但遇到了一个未处理的异常错误。我构造了与教程页面相同的代码,因此我很难找出错误的来源。不确定是否需要修改sql连接。感谢您的帮助。谢谢

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.  
模型:
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间MvcAjax.Models
{
公营雇员
{
公共字符串名称{get;set;}
公共字符串City{get;set;}
公共字符串地址{get;set;}
}
}
视图:
@{
ViewBag.Title=“AddEmployee”;
}
$(文档).ready(函数(){
//函数将在具有id btnsave的按钮单击时调用
$(“#btnSave”)。单击(函数(){
$.ajax(
{
类型:“POST”,//HTTP POST方法
url:“Home/AddEmployee”,//控制器/视图
数据:{//正在传递数据
名称:$(“#txtName”).val(),//使用Jquery读取文本框值
城市:$(“#txtAddress”).val(),
地址:$(“#txtcity”).val()
}
});
});
});


名称 住址 城市
控制器: 使用MvcAjax.Models; 使用制度; 使用System.Collections.Generic; 使用系统配置; 使用系统数据; 使用System.Data.SqlClient; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc; 名称空间MvcAjax.Controllers { 公共类HomeController:控制器 { 私有连接; //获得:AddEmployee 公共行动结果增聘员工() { 返回视图(); } //Post方法以添加详细信息 [HttpPost] 公共行动结果添加员工(员工obj) { 添加详细信息(obj); 返回视图(); } //处理与连接相关的活动 专用无效连接() { string constr=ConfigurationManager.ConnectionString[“SqlConn”].ToString(); con=新的SqlConnection(cont); } 私有无效添加详细信息(员工obj) { 连接(); SqlCommand com=新的SqlCommand(“addamp”,con); com.CommandType=CommandType.StoredProcess; com.Parameters.AddWithValue(“@Name”,obj.Name); com.Parameters.AddWithValue(“@City”,obj.City); com.Parameters.AddWithValue(“@Address”,obj.Address); con.Open(); com.ExecuteNonQuery(); con.Close(); } } } 错误: http://localhost:99999/Home/AddEmployee 未找到视图“AddEmployee”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下地点: ~/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 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.InvalidOperationException:未找到视图“AddEmployee”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下地点: ~/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 源错误: 在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
如果您在写一篇文章,希望得到发回的结果,请返回Json而不是视图

return Json(new {Success = true});
或者你想返回的任何状态。如果返回浏览器的web视图,则返回视图效果良好

请记住,如果希望使用GET返回JSON数据,则需要以不同的方式构造return语句

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

如果您正在执行一个post,希望返回post的结果,那么返回Json而不是视图

return Json(new {Success = true});
或者你想返回的任何状态。如果返回浏览器的web视图,则返回视图效果良好

请记住,如果希望使用GET返回JSON数据,则需要以不同的方式构造return语句

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

我认为你没有在适当的位置看到AddEmployee。它应该放在
Views\Home
文件夹中

请确保按照@Anthony的建议更新您的操作

return Json(new {Success = true});

我认为你没有在适当的位置看到AddEmployee。它应该放在
Views\Home
文件夹中

请确保按照@Anthony的建议更新您的操作

return Json(new {Success = true});

至于你的信息

http://localhost:99999/Home/AddEmployee
只是一个httpget请求,但您将方法修饰为

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

            return View();
        }
httppost带有
Employee
对象。因此出现错误

编辑你的帖子应该像这样返回

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

            return Json(obj);
        }

而不是ActionResult。

关于您的信息

http://localhost:99999/Home/AddEmployee
只是一个httpget请求,但您将方法修饰为

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

            return View();
        }
httppost带有
Employee
对象。因此出现错误

编辑你的帖子应该像这样返回

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

            return Json(obj);
        }

而不是ActionResult。

我认为您在适当的位置没有AddEmployee视图。它应该放在