MVC 和 jQuery 模板?

发布于 2024-10-11 19:04:37 字数 198 浏览 2 评论 0原文

我有一个控制器,它将自定义模型返回到其视图。该模型中包含在视图中呈现的产品和价格的集合,以及页面所需的其他信息。

不过,我想使用 jQuery 模板来允许 AJAX 更新这些产品和价格。

我想不可能用初始值渲染模板,那么如何返回一个既包含“静态”信息(即不会在页面上更改)又包含我确实希望包含在模板中的信息(因为它会发生变化)的模型取决于用户的输入?

I have a controller that returns a custom model back to its view. Within this model is a collection of products and prices which are rendered in the view, alongside other information that is required by the page.

However, I'd like to use jQuery templates to allow an AJAX update of those products and prices.

I guess its not possible to render the template with initial values so how do I return a model that includes both "static" information (ie wont chnage on the page) and also information that I do want to include in the template as it will change dependent on input of the user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宁愿没拥抱 2024-10-18 19:04:37

您可以通过 jquery 从您的控件加载内容,如下所示:

Jquery:

function lastPostFunc(movies) {

            $.each(movies, function(i) {

                var milli = this.ReleaseDate.replace(/\/Date\((-?\d+)\)\//, '$1');
                var d = new Date(parseInt(milli));

                $("#Movie_list").append("<tr class=wrdLatest id=" + this.Id + "><td>" + this.Id + "</td><td>" + this.Title + "</td><td>" + d + "</td><td>"
         + this.Genre + "</td><td>" + this.Price + "</td><td>" + this.Rating + "</td></tr>");
            });

        }
        $(window).scroll(function() {
            if ($(window).scrollTop() + 100 > $(document).height() - $(window).height()) {

                var name = id; //$(".wrdLatest:last").attr("id");
                var value = { skip: name };
                $.post("/Movies/GetMovies/", value, lastPostFunc, "json");
                id += 50;
            }
        }); 

控制器

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetMovies(string skip)
        {
            // caching the categories 

            int idtoSkip = Convert.ToInt32(skip);
            var movies = from m in _db.Movie
                         where m.Id > idtoSkip & m.Id < idtoSkip + 50
                         select m;
            return Json(movies);
        }

You can load content via jquery from your control like this:

Jquery:

function lastPostFunc(movies) {

            $.each(movies, function(i) {

                var milli = this.ReleaseDate.replace(/\/Date\((-?\d+)\)\//, '$1');
                var d = new Date(parseInt(milli));

                $("#Movie_list").append("<tr class=wrdLatest id=" + this.Id + "><td>" + this.Id + "</td><td>" + this.Title + "</td><td>" + d + "</td><td>"
         + this.Genre + "</td><td>" + this.Price + "</td><td>" + this.Rating + "</td></tr>");
            });

        }
        $(window).scroll(function() {
            if ($(window).scrollTop() + 100 > $(document).height() - $(window).height()) {

                var name = id; //$(".wrdLatest:last").attr("id");
                var value = { skip: name };
                $.post("/Movies/GetMovies/", value, lastPostFunc, "json");
                id += 50;
            }
        }); 

Controller

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult GetMovies(string skip)
        {
            // caching the categories 

            int idtoSkip = Convert.ToInt32(skip);
            var movies = from m in _db.Movie
                         where m.Id > idtoSkip & m.Id < idtoSkip + 50
                         select m;
            return Json(movies);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文