Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Engan 430 posts 1173 karma points
    Mar 09, 2016 @ 14:52
    Tom Engan
    0

    How to put in repository classes into SurfaceController?

    I want to find out how to build form with best practice and surfacecontroller by using an example ( http://www.computermagic.gr/tutorials/umbraco-7/custom-tables-with-petapoco/introduction/ ), I would like to add this example with a controller.

    Created me a model:

    using System;
    using System.ComponentModel.DataAnnotations;
    using Umbraco.Core.Persistence;
    using Umbraco.Core.Persistence.DatabaseAnnotations;
    
    namespace BestPractice.RegForms.Models
    {
        [TableName("CMAuthors")]
        [PrimaryKey("AuthorID", autoIncrement = true)]
        [ExplicitColumns]
        public class AuthorViewModel
        {
            [Column("AuthorID")]
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int AuthorID { get; set; }
    
            [Column("Name")]
            [Length(20)]
            public string Name { get; set; }
    
            [Column("Surname")]
            [Length(20)]
            public string Surname { get; set; }
    
            [Column("DateOfBirth")]
            public DateTime DateOfBirth { get; set; }
    
    
        }
    }
    

    And a Repository (like in the example):

    using BestPractice.RegForms.Models;
    using System.Collections.Generic;
    using Umbraco.Core.Persistence;
    
    namespace BestPractice.RegForms.Repository
    {
        public static class AuthorsRepository
        {
            public static IList<AuthorViewModel> GetAll()
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                return db.Fetch<AuthorViewModel>("SELECT * FROM CMAuthors ORDER BY Surname, Name");
            }
    
            public static Page<AuthorViewModel> GetAllPaged(int Page, int RecordsPerPage)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                return db.Page<AuthorViewModel>(Page, RecordsPerPage, "SELECT * FROM CMAuthors ORDER BY Surname, Name");
            }
    
            public static AuthorViewModel GetByAuthorID(int AuthorID)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                List<AuthorViewModel> Records = db.Fetch<AuthorViewModel>("SELECT * FROM CMAuthors WHERE AuthorID = @0", AuthorID);
    
                if (Records.Count > 0)
                    return Records[0];
                else
                    return null;
            }
    
            public static AuthorViewModel Save(AuthorViewModel item)
            {
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                db.Save(item);
                return item;
            }
    
            public static int DeleteByAuthorID(int AuthorID)
            {
                //UmbracoDatabase _database = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                //return _database.Execute("DELETE FROM CMAuthors WHERE AuthorID = @0", AuthorID);
                UmbracoDatabase db = Umbraco.Core.ApplicationContext.Current.DatabaseContext.Database;
                return db.Execute("DELETE FROM CMAuthors WHERE AuthorID = @0", AuthorID);
            }
        }
    }
    

    The controller:

    using BestPractice.RegForms.Models;
    using BestPractice.RegForms.Repository;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace BestPractice.RegForms.Controllers
    {
        public class AuthorSurfaceController : SurfaceController
        {
            /// <summary>
            /// Renders the Author Form
            /// @Html.Action("AuthorForm","AuthorSurface");
            /// </summary>
            /// <returns></returns>
            public ActionResult RenderAuthorForm()
            {
                //Return partial view /views/Partials/AuthorForm.cshtml 
                //(should it be /views/AuthorSurface/AuthorForm.cshtml)?
                //with an empty/new AuthorViewModel
                return PartialView("AuthorForm", new AuthorViewModel());
    
                //return PartialView("AuthorForm", AuthorsRepository.GetAll());
                //return PartialView("AuthorForm", AuthorsRepository.GetByAuthorID(1));
                //return PartialView("AuthorForm", AuthorsRepository.DeleteByAuthorID(1));
            }
        }
    }
    

    Here are the (partial) view:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<BestPractice.RegForms.Models.AuthorViewModel>
    
    @if (TempData["success"] == null)
    {
        using (Html.BeginUmbracoForm<BestPractice.RegForms.Controllers.AuthorSurfaceController>("RenderAuthorForm"))
        {
            @Html.AntiForgeryToken()
    
            @Html.TextBoxFor(m => m.Name, new { @Class = "name", placeholder = "Navn" })
            @Html.ValidationMessageFor(m => m.Name)
            <br />
    
            @Html.TextBoxFor(m => m.Surname, new { @Class = "surname", placeholder = "Etternavn" })
            @Html.ValidationMessageFor(m => m.Surname)
            <br />
    
            @Html.TextBoxFor(m => m.DateOfBirth, "{0:dd.MM.yyyy}", new { @Class = "dateofbirth", placeholder = DateTime.Now })
            @Html.ValidationMessageFor(m => m.DateOfBirth)
            <br />
    
            <button id="contact-submit" type="submit">Send</button>
        }
    }
    else
    {
        <p>Ny forfatter er registrert</p>
    }
    

    And macro to retrieve partial view (into the content in a template):

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @Html.Action("RenderAuthorForm", "AuthorSurface")
    

    How can all classes from the repository be inserted into the controller, and how can all these classes be used in the (partial) view, and is this a good way to do this (I want to develope forms with CRUD from members in a universal method that works in both small and large solutions).

  • Aristotelis Pitaridis 82 posts 400 karma points
    Mar 09, 2016 @ 17:01
    Aristotelis Pitaridis
    0

    First of all thank you for visiting my web site. You have mixed the code from the tutorial that I have written with an example of how to create a table and access the database with the code that generated with the form generate tool that I have created.

    The form generator generates a from for submitting data without doing something with these data. In the controller I have a comment asking you to use the model. This is the place that you can use the repository and store the data that the user typed. This functionality is for inserting a record in the database. If you want to implement the other functions like editing, displaying the records or deleting a record you will have to write new code to do such a thing.

  • Tom Engan 430 posts 1173 karma points
    Mar 10, 2016 @ 12:02
    Tom Engan
    0

    Great website you have that I'm going to use more. I wrote the forms manually, not with your tool, but now that I have become aware of the tool, I'm going to use it for new forms. Good job you've done there!

    I also think there is much good to learn about Umbraco on your website, and I'll bookmark it as an Umbraco resource. I'm trying to find a universal method for developing forms that can be used for both large and small registration system integrated in Umbraco. The method used in Example http://www.computermagic.gr/tutorials/umbraco-7/custom-tables-with-petapoco/insert-data/ adds data fast, but I would like to see how you can use forms with user input instead of (and eventually integrated with Umbraco membership).

    I can't find any comments in the controller (I think I saw someone somewhere else on the web in a similar example). Normally I'm referring to a (view)model in a controller, but I would like to see the code on the controller where all the methods from the reposotory has been used. Not only adding, but also to modify and delete records.

    Much is new regarding the expansion of Umbraco with forms, and I try to experiment with different methods. Probably should it be at least two methods, both with and without Umbraco ContentService to be associated with the Content-nodes in Umbraco (if I understand this right).

    But my question now is: How will this controller that uses reposotory look in just this example, and can all the methods from the controller be used in the same view (as I believe)?

  • Aristotelis Pitaridis 82 posts 400 karma points
    Mar 10, 2016 @ 12:31
    Aristotelis Pitaridis
    0

    What I do is to create a different view for each of these functions. This is how it is supposed to be done. If you want everything to happen in one view then you could do it using ajax calls. You will have a view with the list of records and the creating, editing and deletion of the records will be done using ajax calls to a controller using JavaScript.

Please Sign in or register to post replies

Write your reply to:

Draft