在使用把手的mvc视图中显示模型信息

时间:2017-12-07 12:19:39

标签: javascript ajax asp.net-mvc handlebars.js

在mvc视图中显示模型信息,我是新手来处理吧。我想用把手显示模型的信息。

我有一个Ajax调用,它返回成功的模型信息,我希望将模板与数据绑定。

但是,我没有成功。

我的努力在下面。

<html lang="en">
<head>

    <script src="~/scripts/jquery-1.10.2.min.js"></script>
    <script src="~/scripts/handlebars.min.js"></script>

    <title> HandleBar Practice</title>
</head>
<body>

    <script id="message-response-template" type="text/x-handlebars-template">

        <div>
            My FirstName Is: {{FirstName}}
        </div>
        <div>
            My MiddleName Is: {{MiddleName}}
        </div>
        <div>
            My Address Is: {{Address}}
        </div>
        <div>
            My PhoneNumber Is: {{PhoneNumber}}
        </div>
        <div>
            My CNIC Is: {{CNIC}}
        </div>



    </script>
</body>
</html>
<script>
    $(document).ready(function () {
        alert('document ready');
        $.ajax({
            type: "POST",
            url: "AjaxCall",
            data: '{ sendData: "ok" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                console.log(result);
                var templateResponse = Handlebars.compile($("#message-response-template").html());
                templateResponse(result);
            }
        });
    });



</script>

输出

浏览器控制台

Handlebars.compile($("#message-response-template").html())
ƒ e(a,b){return g||(g=d()),g.call(this,a,b)}

var templateResponse = Handlebars.compile($("#message-response-template").html());

undefined //it display undefined i am not getting the idea what's wrong

来源

[HttpPost]
        public ActionResult AjaxCall(string sendData)
        {

            return Json(new Employee() { FirstName = "John", MiddleName = "N/A", LastName = "DOE", CNIC = "123234234", Address = "Pakistan" }, JsonRequestBehavior.AllowGet);
        }

0 个答案:

没有答案