1

I'm trying to create a simple MVC App to insert data into a db using AJAX Post method and am getting an unhandled exception error. I structured the code identical to the tutorial page, so I am having some trouble figuring out where the error is coming from. Not sure if I possibly need to modify the sql connection. Any help is appreciated. Thanks!

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.  
2
  • 1
    I think you don't have AddEmployee view in proper location. It should be placed in Views\Home folder. Commented Apr 17, 2016 at 15:13
  • Worked perfectly! It doesn't seem to update my db once 'Save' btn is clicked, so working on debugging that now. That probably has more to do w my connection string. Thanks!! Commented Apr 17, 2016 at 15:25

3 Answers 3

2

If you're doing a post expecting the results of the POST back, return Json rather than a View

return Json(new {Success = true});

or whatever status you want to return. Returning a View works well if you're returning a web view for a browser.

Keep in mind, if you want to return JSON data with a GET, you'll need to construct your return statement differently

return Json(new {Success = true}, JsonRequestBehavior.AllowGet);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I am just learning the basics of the Post, but am having trouble getting the data submitted to the db. I believe that is more the connection string I'm working with. Thanks again.
2

I think you don't have AddEmployee view in proper location. It should be placed in Views\Home folder.

Please make sure you update your Action as @Anthony has suggested.

return Json(new {Success = true});

1 Comment

Thanks. Just made the update and am still learning the differences of returning a View vs a Json. Thank you for the help on this, I am clearly a beginner.
0

As for your information

http://localhost:99999/Home/AddEmployee

is simply a HTTP GET request but you decorate your method as

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

            return View();
        }

HTTP POST with Employee object.So there is an error.

EDIT your post should return like this

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

            return Json(obj);
        }

Instead of ActionResult.

1 Comment

Please have a look at the error, it is related to view not HttpMethods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.