Copied to clipboard

Flag this post as spam?

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


  • Rahulk 24 posts 54 karma points
    Nov 15, 2016 @ 14:36
    Rahulk
    0

    How to get value from umbraco7 using C# code

    I have created a controller class and model class. Within in the model class I have written below method to get values from umbraco.

    public void GettingValueFromUmbraco(IPublishedContent content)
    {
       Name = content.GetProperty("EmpName").Value.ToString();
       Email = content.GetProperty("email").Value.ToString();
       Mobile = content.GetProperty("mobile").Value.ToString();
    }
    

    And I have called this method in Controller class.

    public ActionResult Home()
    {        
       UmbracoTest umbracoTest = new UmbracoTest();
       umbracoTest.GettingValueFromUmbraco();
       return View();
    }
    

    From controller we want to pass parameter value. Anybody please help me to pass the right value to the function.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 14:44
    Alex Skrypnyk
    0

    Hi Rahulk,

    Welcome to our community!!!!

    Can you explane why do you need to get data in controller and push it to view?

    With Umbraco you can access Umbraco related data directly in view, especially if it's current page data.

    Did you look at documentation?

    https://github.com/umbraco/UmbracoDocs/tree/master/Tutorials/Creating-Basic-Site

    https://our.umbraco.org/documentation/

    And of course ask, ask and ask. We are the friendliest community!

    Thanks,

    Alex

  • Rahulk 24 posts 54 karma points
    Nov 15, 2016 @ 14:53
    Rahulk
    0

    Hi Alex,

    Thank you for your quick replay.

    I am try to learn how I can get values from umbraco it into Model class. Just for learning umbraco things and C#

    Please help me to pass the right parameter for this context.

    Thanks Rahul

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 14:59
    Alex Skrypnyk
    0

    Hi Rahul,

    Umbraco makes strongly typed models for you, just look at https://github.com/zpqrtbnk/Zbu.ModelsBuilder/wiki/Zbu.ModelsBuilder

    Thanks,

    Alex

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 15:11
    Alex Skrypnyk
    100

    You don't need to write such code at all:

    Name = content.GetProperty("EmpName").Value.ToString();
       Email = content.GetProperty("email").Value.ToString();
       Mobile = content.GetProperty("mobile").Value.ToString();
    

    You already have strongly typed model of page, just render it in Razor:

    Model.Name
    

    Thanks,

    Alex

  • Rahulk 24 posts 54 karma points
    Nov 15, 2016 @ 15:42
    Rahulk
    0

    You are right. In razor I can render like that. But how I can access it in C# for doing some business logic in model class.

    Thanks Rahul

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 15, 2016 @ 16:10
    Alex Skrypnyk
    0

    Hi Rahul,

    What exactly logic do you want to do?

    Some read or write operations?

    Thanks,

    Alex

  • Charlie Haley 7 posts 119 karma points
    Nov 15, 2016 @ 20:23
    Charlie Haley
    0

    Hi Rahul,

    I think you might be looking at RenderModel

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

    If you make your controller have the base class of RenderMvcController and pass RenderModel into your method you'll be able to access IPublishedContent from there.

    public ActionResult Index(RenderModel umbracoModel)
    {
            var model = umbracoModel.Content.GetProperty("email");
    }
    

    Umbraco works on a naming convention basis so make sure that your Document Type has the same name as your controller.

    Hope this helps!

  • Rahulk 24 posts 54 karma points
    Nov 16, 2016 @ 15:53
    Rahulk
    0

    Hi Charlie,

    Thank you very much.

    Exactly am looking this one. As per your suggestion I have amened my code. Please help me to know is this the right way to implemet?

    public class HomeController : Umbraco.Web.Mvc.RenderMvcController
        {
            public ActionResult Home(RenderModel renderModel)
            {
                UmbracoTest umbracoTest = new UmbracoTest();
                umbracoTest.GettingValueFromUmbraco(renderModel);
                return View();
            }
        }
    
    namespace UmbracoStudy.Models
    {
        public class UmbracoTest
        {
    
            public string Name { get; set; }
            public string Email { get; set; }
            public string Mobile { get; set; }
    
            public void GettingValueFromUmbraco(RenderModel content)
            {
                Name = content.Content.GetProperty("EmpName").Value.ToString();
                Email = content.Content.GetProperty("email").Value.ToString();
                Mobile = content.Content.GetProperty("mobile").Value.ToString();
            }
        }
    }
    

    Thanks Rahul

  • Charlie Haley 7 posts 119 karma points
    Nov 16, 2016 @ 16:40
    Charlie Haley
    0

    Hi Rahul,

    That looks like it would work to me! You're most likely not going to need to pass the entire RenderModel through, so just do renderModel.Content like below.

    public class HomeController : Umbraco.Web.Mvc.RenderMvcController
    {
        public ActionResult Home(RenderModel renderModel)
        {
            UmbracoTest umbracoTest = new UmbracoTest();
            umbracoTest.GettingValueFromUmbraco(renderModel.Content);
            return View();
        }
    }
    
    public class UmbracoTest
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
    
        public void GettingValueFromUmbraco(IPublishedContent content)
        {
            Name = content.GetProperty("EmpName").Value.ToString();
            Email = content.GetProperty("email").Value.ToString();
            Mobile = content.GetProperty("mobile").Value.ToString();
        }
    }
    

    Charlie

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 16, 2016 @ 16:45
    Alex Skrypnyk
    0

    Hi Charlie and Rahul,

    Also you can use constructor:

    public class HomeController : Umbraco.Web.Mvc.RenderMvcController
    {
        public ActionResult Home(RenderModel renderModel)
        {
            UmbracoTest umbracoTest = new UmbracoTest(renderModel.Content);
    
            return View("/views/umbracoTest.cshtml", umbracoTest);
        }
    }
    
    namespace UmbracoStudy.Models
    {
        public class UmbracoTest
        {
    
            public string Name { get; set; }
            public string Email { get; set; }
            public string Mobile { get; set; }
    
            public UmbracoTest(IPublishedContent content)
            {
                Name = content.GetProperty("EmpName").Value.ToString();
                Email = content.GetProperty("email").Value.ToString();
                Mobile = content.GetProperty("mobile").Value.ToString();
            }
        }
    }
    

    Can you show code of your view? Because I don't see why we need controller now.

    Thanks,

    Alex

  • Rahulk 24 posts 54 karma points
    Nov 18, 2016 @ 05:04
    Rahulk
    0

    Thank you Charlie.

    Thanks & Regards Rahul

  • Rahulk 24 posts 54 karma points
    Nov 18, 2016 @ 05:05
    Rahulk
    0

    Hi Alex,

    As of now I didn't write the code for view. Definitely will share you once it is done or start the code.

    Thanks & Regards Rahul

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 18, 2016 @ 10:56
    Alex Skrypnyk
    0

    Glad to hear, wait for your reply.

  • Rahulk 24 posts 54 karma points
    Nov 18, 2016 @ 13:10
    Rahulk
    0

    Hi Alex, Charlie

    Below I have copied the code written in Controller, Model and View.

    Controller:

    public ActionResult Home(RenderModel renderModel)
            {
                UmbracoTest umbracoTest = new UmbracoTest();
                var testModel=umbracoTest.GettingValueFromUmbraco(renderModel.Content);
                return View("/Views/Home/Index.cshtml",testModel);
            }
    

    Model:

    public UmbracoTest GettingValueFromUmbraco(IPublishedContent content)
            {
                UmbracoTest umb = new UmbracoTest
                {
                    Name = content.GetProperty("EmpName").Value.ToString(),
                    Email = content.GetProperty("email").Value.ToString(),
                    Mobile = content.GetProperty("mobile").Value.ToString()
                };
    
                return umb;
            }
    

    View:

    @model UmbracoStudy.Models.UmbracoTest
    
        <div class="row">
            <p>Employee Name: @Model.Name</p>
            <p>Employee Email: @Model.Email</p>
            <p>Employee Mobile: @Model.Mobile</p>   
        </div>
    

    Thanks & Regards Rahul

Please Sign in or register to post replies

Write your reply to:

Draft