Copied to clipboard

Flag this post as spam?

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


  • Edgar Arroyo 19 posts 161 karma points
    Jun 08, 2019 @ 04:45
    Edgar Arroyo
    0

    Can I access a model's property from a Web Form?

    Ok, so I can access a model's property with @Model.Value("propertyName")

    My new challenge is... I have a C# Web Form and I want to access a model property I added, is this possible?

    For testing purposes, I added a property AdminID of type Umbraco.TextBox and I gave it a value of 1 on my Document Type "Home."

    On my template I access the model:

    @{
        var home = (Home)Model.Root();
        var adminID = home.Value("adminID");
    }
    <script>alert('@adminID');</script>
    

    And I get an alert box with a 1 in it. Everything is fine... Now I want to access this value from a C# Web Form. Is this possible?

  • Edgar Arroyo 19 posts 161 karma points
    Jun 10, 2019 @ 04:19
    Edgar Arroyo
    100

    Well, I got no answer, so I don't know of any "clean" way... I ended up doing it via SQL...

    select varcharValue from umbracoContentVersion a
    inner join umbracoPropertyData b on b.versionId = a.id
    inner join cmsPropertyType c on c.id = b.propertyTypeId
    where [current] = 1 and [text] = 'Home' and [alias] = 'adminID'
    

    This seems to work for me for now, marking as solution, but even though this works I would like a more elegant way of doing it.

    Any advice?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 10, 2019 @ 08:05
    Marc Goodson
    0

    Hi Edgar

    It's rare you need to diving for data with direct SQL requests to the dB...

    When you say c# 'web form' - do you mean .net WebForms approach to building templates? Eg .aspx pages and masterpages? Support for WebForms has been dropped in V8...

    ..or...

    Do you mean a form in a partial view that your trying to read the value after posting...

    If you can describe more of what you have codewise and are trying to achieve, then suspect there will be a less murky way we can find to achieve it!

    Regards

    Marc

  • Edgar Arroyo 19 posts 161 karma points
    Jun 10, 2019 @ 12:04
    Edgar Arroyo
    0

    Yes, a .Net C# WebForm. I access it with an Ajax post to do behund-the-scenes stuff and I want to use a property value in Home. Is there a way to reference the Home Model from an outside .Net C# WebForm?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jun 10, 2019 @ 13:54
    Marc Goodson
    0

    Hi Edgar

    Ok, intriguing!

    I'd probably create an UmbracoApiController

    https://our.umbraco.com/Documentation/Reference/Routing/WebApi/

    And post your model here - you would have access to the usual Umbraco Helpers inside the API Controller, so easy to pull back content from Umbraco - and write any c#-stuff you need to do in background...

    or if it MUST be a .net webforms aspx form and this is separate from Umbraco - you could have an UmbracoApiController that the Webforms project posts to, to retrieve data from Umbraco.

    or (and I haven't tried this) if you have the Core + Web Nuget packages installed in a separate class library project - your WebForms project could reference this and the Umbraco connection string, you could create a service to use the Umbraco Helpers... to be your gateway to Umbraco content.

    IEnumerable<IPublishedContent> rootSites = Umbraco.ContentAtRoot();
    

    will get you a list of the sites at the root of your Umbraco site, and presumably where your homepage lives, or

    IPublishedContent homePage = Umbraco.Content(1234); 
    

    would get you a specific page, if you post the id of the homepage to your WebForm...

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft