c# – 无法使用json在视图中的html表中显示员工详细信息

当用户单击视图上的按钮时,我在显示员工详细信息时遇到问题.

当用户点击按钮时,员工详细信息(包括id和名称)将通过
JSON调用并在html表中显示数据,为此我在我的视图中这样写了

@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
    $(function () {
        $('#submitbtnn').click(function () {
            var table = $('#parenttable');
            var url = '/EmpDetails/GetEmployees/';
            $.getJSON(url, function (data) {
                $.each(data, function (key, Val) {
                    var user = '<tr><td>' + Val.EmployeeId + '<td><tr>' + '<tr><td>' + Val.EmployeeName + '<tr><td>'
                    table.append(user);
                });
            });
        });
    });
</script>    
@{
    ViewBag.Title = "GetEmployeesByName";
}
<h2>GetEmployeesByName</h2>
@using (Html.BeginForm())
{ 
    <table id ="parenttable"></table>    
    <input id="submitbtnn" type="Submit" value="Submit1" />       
}

这是我的控制器,这里我将返回json数据进行查看

namespace MvcSampleApplication.Controllers
{
    public class EmpDetailsController : Controller
    {            
        [HttpGet]
        public ActionResult GetEmployees()
        {
            List<EmployeeClass> employees = new List<EmployeeClass>();
            EmployeeClass employee1 = new EmployeeClass { EmployeeId=1, EmployeeName = "Rams"};
            EmployeeClass employee2 = new EmployeeClass { EmployeeId = 2, EmployeeName = "joseph" };
            EmployeeClass employee3 = new EmployeeClass { EmployeeId = 3, EmployeeName = "matt" };
            employees.Add(employee1);
            employees.Add(employee2);
            employees.Add(employee3);    
            return Json(employees, JsonRequestBehavior.AllowGet);
        }    
    }
}

但我正在尝试访问此URL时,我收到http 404资源未找到错误

http://localhost/EmpDetails

任何人都会建议任何想法和任何建议,这对我来说是非常好的..

非常感谢…
修改后的视图

   @Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
    $(function () {
        $('#submitbtnn').click(function () {            
            var table = $('#parenttable');
            var url = @Url.Action("GetEmployees","EmpDetails")      
           //alert("hi");
            $.getJSON(url, function (data) {
                //alert("hi");
                $.each(data, function (Val) {                       
                    var user = '<tr><td>' + Val.EmployeeId + Val.EmployeeName + '<tr><td>'
                    table.append(user);
                });                   
            });
            return false;
        });
    });
</script>
@{
    ViewBag.Title = "GetEmployeesByName";
}
<h2>GetEmployeesByName</h2>
@using (Html.BeginForm())
{ 
    <div class="temptable">
       <table id ="parenttable"></table>    
    </div>   
    <input id="submitbtnn" type="Submit" value="Submit1" />
}

我无法在JavaScript函数中点击第二个警报功能.

最佳答案 您忘记通过从单击处理程序返回false来取消该按钮的默认操作:

$('#submitbtnn').click(function () {
    var table = $('#parenttable');
    var url = '/EmpDetails/GetEmployees/';
    $.getJSON(url, function (data) {
        $.each(data, function (key, Val) {
            var user = '<tr><td>' + Val.EmployeeId + '<td><tr>' + '<tr><td>' + Val.EmployeeName + '<tr><td>'
            table.append(user);
        });
    });

    return false; // <!-- This is very important
});

通过不取消默认操作,当您单击提交按钮时,浏览器会提交表单并从页面重定向到表单的操作,从而没有时间让AJAX请求执行.

点赞