簡體   English   中英

ASP.NET MVC jQuery發布錯誤

[英]ASP.NET MVC jQuery Post Error

我正在嘗試創建一個簡單的MVC App,以使用AJAX Post方法將數據插入db,並且遇到未處理的異常錯誤。 我的代碼結構與教程頁面相同,因此在弄清錯誤的出處時遇到了一些麻煩。 不知道我是否可能需要修改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.  

如果您要發布的帖子期待POST的返回結果,請返回Json而不是View

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

至於你的資料

http://localhost:99999/Home/AddEmployee

只是一個HTTP GET請求,但是您將方法裝飾為

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

            return View();
        }

帶有Employee對象的HTTP POST 。因此出現錯誤。

編輯您的帖子應該像這樣返回

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

            return Json(obj);
        }

而不是ActionResult。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM