Mohammadmk says:
data: { employeesViewModel: JSON.stringify(employeeObject) },
Replace the above with the below code.
data: employeeObject,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
Model
public class EmployeesViewModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
NorthwindEntities db = new NorthwindEntities();
public ActionResult Index()
{
List<EmployeesViewModel> employees = db.Employees
.Select(x => new EmployeesViewModel
{
Id = x.EmployeeID,
FirstName = x.FirstName,
LastName = x.LastName,
City = x.City,
Country = x.Country
}).ToList();
return View(employees);
}
[HttpGet]
public ActionResult GetEmployees(EmployeesViewModel employeesViewModel)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString))
{
List<EmployeesViewModel> employees = new List<EmployeesViewModel>();
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId,FirstName,LastName,City,Country FROM Employees WHERE EmployeeId = @ID AND FirstName = @NAME", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@NAME", string.IsNullOrWhiteSpace(employeesViewModel.FirstName) ? null : employeesViewModel.FirstName);
cmd.Parameters.AddWithValue("@ID", string.IsNullOrWhiteSpace(employeesViewModel.Id.ToString()) ? null : employeesViewModel.Id.ToString());
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
EmployeesViewModel employee = new EmployeesViewModel();
employee.Id = Convert.ToInt32(reader["EmployeeId"]);
employee.FirstName = reader["FirstName"].ToString();
employee.LastName = reader["LastName"].ToString();
employee.City = reader["City"].ToString();
employee.Country = reader["Country"].ToString();
employees.Add(employee);
}
}
}
return PartialView("_EmployeeDetails", employees);
}
}
}
Index View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<_203261_Filter_WebAPI.Models.EmployeesViewModel>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript">
$(document).ready(function () {
$("#btnSearch").click(function () {
var employeeObject = { Id: $("#txtId").val(), FirstName: $("#txtName").val() };
$.ajax({
type: 'GET',
url: '/Home/GetEmployees',
data: employeeObject,
dataType: "html",
contentType: 'application/json; charset=utf-8',
success: function (data) {
$('#dvPartial').html(data);
},
error: function (err) {
}
});
});
});
</script>
</head>
<body>
<table class="table table-bordered">
<tr>
<th>
Id
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
City
</th>
<th>
Country
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.Id %>
</td>
<td>
<%: item.FirstName %>
</td>
<td>
<%: item.LastName %>
</td>
<td>
<%: item.City %>
</td>
<td>
<%: item.Country %>
</td>
</tr>
<% } %>
</table>
<br />
<div class="row">
<div class="form-group">
<div class="col-sm-3">
<input type="text" id="txtId" class="form-control" value="1" />
</div>
<div class="col-sm-3">
<input type="text" id="txtName" class="form-control" value="Nancy" />
</div>
<div class="col-sm-3">
<button id="btnSearch" class="btn btn-primary">
Search</button>
</div>
</div>
</div>
<br />
<hr />
<h3>
Search Result
</h3>
<div id="dvPartial">
</div>
</body>
</html>
_EmployeeDetails PartialView
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<_203261_Filter_WebAPI.Models.EmployeesViewModel>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>_EmployeeDetails</title>
</head>
<body>
<table class="table table-bordered">
<tr>
<th>
Id
</th>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
City
</th>
<th>
Country
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.Id %>
</td>
<td>
<%: item.FirstName %>
</td>
<td>
<%: item.LastName %>
</td>
<td>
<%: item.City %>
</td>
<td>
<%: item.Country %>
</td>
</tr>
<% } %>
</table>
</body>
</html>
Screenshot
