Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Jul 17, 2014 @ 17:21
    Jason Espin
    0

    Accessing dictionary helper to retrieve values using a custom model

    Hi all,

    I'm having a bit of trouble creating a Surface controller that validates and processes an enquiry form. I wish to make the display names in my model editable through the Umbraco dictionary so that we can cater for multi-lingual support at a later date.

    My model is as follows:

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Web;
    
    namespace project.Models
    {
    
        public class EnquiryModel
        {
          [Required]
          [MaxLength(30)]
          [Display(Name="First Name")]
    
          public string First_Name {get; set;}
    
          ... 
        }
    
    }
    

    I want to inject the relevant dictionary value from the Umbraco back office into the Display field but so far I have been unable to do so.

    My controller is as follows:

    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using project.Models;
    
    namespace project.Controllers
    {
      public class EnquiryController : SurfaceController
      {
        [HttpPost]
        public ActionResult Submit(EnquiryModel model)
        {
          if (!ModelState.IsValid)
             return CurrentUmbracoPage();
    
          return RedirectToCurrentUmbracoPage();
        }
      }
    }
    

    And my view is as follows:

    @using project.Controllers
    @model project.Models.EnquiryModel
    
    @using (Html.BeginUmbracoForm<EnquiryController>("Submit"))
    {
       <div class="form-group col-md-6">
          @Html.LabelFor(model => model.First_Name, new { @class = "control-label"})
          @Html.TextBoxFor(model => model.First_Name, new { @class = "form-control"})
       </div>
    }   
    

    I could technically inject the relevant dictionary values within the view however, because I have a model using statement I cannot inherit from an UmbracoTemplatePage. Because of this, I also had to place my form in a Macro before it would render.

    How would I go about accessing the Umbraco dictionary to perform what I need to do?

    I'd assume it should be as easy as using the correct namespace in the Model but as with most cases in Umbraco i'm sensing it is slightly more complicated than that.

    Any help would be greatly appreciated. I am using the latest version of Umbraco.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 18, 2014 @ 10:29
    Ismail Mayat
    1

    Jason,

    What you want todo is on your model for the labels use items from the umbraco dictionary? Is that correct? If so then you need to take a look at this blog post http://creativewebspecialist.co.uk/2014/04/17/umbraco-mvc-regionalisation-of-error-messages/ also you can take a look at something i did a while back which is very similar https://github.com/ismailmayat/MvcUmbracoDataAnnotations

    Regards

    Ismail

  • Jason Espin 368 posts 1335 karma points
    Jul 18, 2014 @ 15:15
    Jason Espin
    0

    This project doesn't seem to work though. I've imported it into my solution in Visual Studio and have then used the following code to reference the Umbraco dictionary:

     [UmbracoRequired("Enquiry.Forename.Error")]
     [UmbracoDisplayName("Enquiry.Forename.Display")]
      public string First_Name { get; set; }
    

    In my dictionary I have created the following structure:

     Enquiry
       Forename
         Error - "This is an error"
         Display - "First Name (From Dictionary)"
    

    But when I go to the page on which my form is displayed, the display name is coming up as :

    First_Name
    

    when it should be:

    First Name (From Dictionary)
    

    This signifies that it isn't even retrieving any values from the Umbraco dictionary.

  • Jason Espin 368 posts 1335 karma points
    Jul 18, 2014 @ 16:45
    Jason Espin
    0

    Okay so I've managed to get this working now pulling through the Dictionary values for error messages etc but it doesn't seem to work with the JQuery validate unobtrusive library despite supposedly being compatible. Any ideas why these custom tags and validation messages are not working with this?

Please Sign in or register to post replies

Write your reply to:

Draft