Ajax with Model
base httppost in mvc?
1. create model
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
2. create controller
[HttpPost]
public ActionResult Emp(Employee emp)
{
return View("Emp",emp);
}
3.Create view with ajax
@html.texboxfor(r=>r.Id,new{@Id=in})
@html.texboxfor(r=>r.Name,new{@Id=nm})
<script>
$(document).ready(function() {
$("#click").click(function () {
var
viewModel = { Id: $("#in").val(),
Name: $("#nm").val() };
$.ajax({
type: "POST",
url: 'http://localhost:11685/Home/Emp', //your action
data: viewModel,//or{Id:$("#in").val(),Name:$("#nm").val()}; model
dataType: 'json',
success: function (result) {
console.log(result);
}
})
return false;
});
});
</script>