问题
I am new to Umbraco and want to perform the following tasks in it.
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.
I don't want to use JQuery, AngularJS etc., but rather to do it on the server side.
Any guidance will be appreciated greatly.
P.S. I have some knowledge about Umbraco fundamentals and its Partial View Macros.
回答1:
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.
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:
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(); } }
Your form needs to post to your SurfaceController action, e.g.
@using(Html.BeginUmbracoForm("SaveMovie", "MovieSurface")) { ... }
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:
- Store the submitted values in a separate database table, i.e. separate from the core Umbraco database tables.
- Retrieve the movie values from this database table directly bypassing Umbraco completely.
You can do this by utilising the Petapoco Database
instance that Umbraco is built upon:
ApplicationContext.DatabaseContext.Database.Save(new Movie());
Or
var item = ApplicationContext.DatabaseContext.Database.Single<Movie>(movieId);
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.
来源:https://stackoverflow.com/questions/25531906/how-to-persist-and-fetch-a-list-in-umbraco