如何在Umbraco中保留和获取列表? [英] How To Persist and Fetch a List in Umbraco?

查看:62
本文介绍了如何在Umbraco中保留和获取列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Umbraco的新手,并希望在其中执行以下任务.

I am new to Umbraco and want to perform the following tasks in it.

  1. 使用输入表单将电影列表存储在数据库中.

  1. Store a list of movies in the database using an entry form.

获取电影的记录,并将其显示在我有模板的页面中.

Fetch the records of the movies and display them in a page for which I have a template.

我不想使用JQuery,AngularJS等,而是想在服务器端使用它.

I don't want to use JQuery, AngularJS etc., but rather to do it on the server side.

任何指导将不胜感激.

P.S.我对Umbraco基础知识及其部分视图宏有一些了解.

推荐答案

为澄清起见,我假设您希望在Umbraco的内容树中创建电影节点,例如在所有电影"页面下.

To clarify, I am assuming that you wish to create movie nodes in Umbraco's content tree, e.g. under a page called "All Movies".

首先,允许网站中的表单直接写入您的内容树是非常的错误做法.这带来了很多问题,例如用户向您的数据库发送垃圾邮件并用垃圾填充它.

Firstly, it is very bad practice to allow a form in your website to write directly to your content tree. This opens up to a whole world of issues like users spamming your db and filling it up with rubbish.

但是,为了使用MVC方法在Umbraco中的表单中创建内容,您需要在 http://our.umbraco.org/documentation/Reference/Templating/Mvc/.总结一下,您需要:

However, in order to create content from a form in Umbraco using the MVC approach, you need to review the documentation here http://our.umbraco.org/documentation/Reference/Templating/Mvc/. To summarise though, you need:

  • SurfaceController上用于处理表单发布请求的动作

  • An action on a SurfaceController for handling your form post requests

public class MovieSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
    [HttpPost]
    public ActionResult SaveMovie(SaveMovieViewModel model)
    {    
        if (!ModelState.IsValid)
            return CurrentUmbracoPage();

        return RedirectToCurrentUmbracoPage();
    }
}

  • 您的表单需要发布到SurfaceController操作中,例如

  • Your form needs to post to your SurfaceController action, e.g.

    @using(Html.BeginUmbracoForm("SaveMovie", "MovieSurface")) { ... }
    

  • 然后在操作中,您需要访问 ContentService 来创建和保存电影内容,将所有电影"页面作为父页面.

  • Then within your action you need to access the ContentService to create and save your Movie content, targeting the "All Movies" page as being the parent page.

    Services.ContentService.CreateContent(
        "Harold and Maude", allMoviesContent, "Movie", 0
    );
    

  • 但是,就像我说的那样,这是一种不好的做法,我仅向您说明.更好的方法是:

    However, like I say this is bad practice and I show you just as an illustration. A much better approach would be to:

    • 将提交的值存储在单独的数据库表中,即与核心Umbraco数据库表分开.
    • 完全绕过Umbraco,直接从此数据库表中检索电影值.

    您可以通过利用构建了Umbraco的Petapoco Database 实例来做到这一点:

    You can do this by utilising the Petapoco Database instance that Umbraco is built upon:

    ApplicationContext.DatabaseContext.Database.Save(new Movie());
    

    var item = ApplicationContext.DatabaseContext.Database.Single<Movie>(movieId);
    

    更好的做法是创建一个backoffce自定义节,在这里您可以审核提交的电影数据(存储在独立表中)并将电影内容从此处保存到内容树中.

    Even better would be to create a backoffce custom section where you could moderate the submitted Movie data (stored in your independent table) and save the Movie content from here into the content tree.

    这篇关于如何在Umbraco中保留和获取列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆