Copied to clipboard

Flag this post as spam?

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


  • kazio0 1 post 21 karma points
    Dec 10, 2012 @ 10:48
    kazio0
    0

    Save and get data from database

    Hay, I create form from ths website: http://www.diplo.co.uk/blog/2012/5/24/creating-an-umbraco-form-using-pure-razor.aspx

    And I have question how to save data from ths form to database and how its get in Razor?

    Umbraco have maybe Linq to database?

    I use Umbraco 4.11.1

  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 11, 2012 @ 23:20
    Jeremy Pyne
    1

    We use exstensive forms for user feedback on out umbraco site.  Here is how we do it. (I'll try to write up something a bit more detailed when I get a chance.)

    • First We create the database table to hold the stored data.  We just createt he tables directly in the Umbraco database and prefix them all with "Form" to identify them.
    • Next I create a LINQ to SQL model in a .Net Class Library(Visual Studio Web Developer 2010 Express, Create a Class Library Project.)
    • In this object you can navigate to the database table and drag it into the data model.  This will build all the DBA code you will need.
    • Note that to ensure your connecting to the correct databse the connection string will need to be set int he code, I do this with a helper calss and use the umbracoDbDSN setting to simplify things.
    • Next compile this library and copy the resulting dll over to your umbraco install.
    • At this point you will be able to save(and read) data from the table we have linked.
    • I build the web form in a macro with normal HTML markup.  You could use .NET USer Controls if you prefer
    • To do the save logic I use the Umbraco Base subsystem witch allows for simple ajax/lightweight backend code.  You could however create the entire form as a User Contol that does a PostBack and contains the save code itself.
    • Furthermore the /Base calls sent out notification emails and other triggered processes.
    • We also have added a Data section to the Umbraco backend that lists all the custom tables and lets the managers view and edit the data.  This portion is accomplished with User Contols for each table that containd a DataGid.  Simple but effective.
  • Jeremy Pyne 106 posts 246 karma points MVP c-trib
    Dec 11, 2012 @ 23:25
    Jeremy Pyne
    0

    Once you have a LINQ model you can display datime form a macro very simply.  Here is a same bit that I use to display a state dropdown.

    Macro

    @using Models;
    var stateViewModel = new StateViewModel();
    @Html.DropDownList(Name, stateViewModel.StatesCode, new { Class = Class, Id = Id })

    View Model in the class library.

    namespace Models
    {
    publicclassStateViewModel
    {
    readonly StatesRepository statesRepository = new StatesRepository();
     
    publicIEnumerable<SelectListItem> States
          {
              get
              {
                  var list = statesRepository.Find().OrderBy(s => s.Code)
                          .Select(i => new SelectListItem() { Value = i.Code, Text = i.Code + " - " + i.Name })
                          .AsEnumerable();
                  return list;
              }
          }
    }
    }
Please Sign in or register to post replies

Write your reply to:

Draft