Copied to clipboard

Flag this post as spam?

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


  • May Zhang 11 posts 61 karma points
    Feb 20, 2023 @ 20:35
    May Zhang
    0

    HtmlHelper is unable to get correct model in Umbraco 10 MVC views

    Issues with customized MyRenderController (RenderController) and template in Umbraco10:

    Template: @inherits UmbracoViewPage

    If using HtmlHelper inside the view, model for HtmlHelper is dynamic instead of MyPage. When hovering 'Html' text over @Html.BeginForm(m => m...), showing "htmlhelper

    unable to access any properties from MyPage.

    But @Model is MyPage

    Is anyone able to use HtmlHelper in Umbraco 10 MVC views?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 21, 2023 @ 09:12
    Huw Reddick
    0

    I think you may should do this

    @inherits UmbracoViewPage<Umbraco.Cms.Web.Common.PublishedModels.MyPage>
    

    also, what do you have your models builder set to? If it is InMemory then you won't have access to the strongy typed model.

  • May Zhang 11 posts 61 karma points
    Feb 21, 2023 @ 16:47
    May Zhang
    0

    Thanks Huw! I did use the following in view (it didn't display correctly for some reason).

    @inherits UmbracoViewPage< MyPage >

    I tried all setting options (Nothing, InMemoryAuto, SourceCodeManual, SourceCodeAuto) for models builder, all with the same result for HtmlHelper -

    error CS1963: An expression tree may not contain a dynamic operation

    PS, we are actually migrating our site from umbraco 8 to 10, all code works fine in umbraco 8.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 22, 2023 @ 12:25
    Huw Reddick
    0

    Unfortunately 8 and 10 are completely different beasts and just because it works in 8 does not mean it will in 10. Having said that, there is no reason what you are trying should not work. Try clearing any temp/cache data and ensure all your using statements are correct.

  • May Zhang 11 posts 61 karma points
    Feb 22, 2023 @ 16:35
    May Zhang
    0

    Understood, our umbraco 10 project was completely re-created for .net 6

    What I meant was - the customized model class, view and controller were re-created the similar way as in Umbraco 8. But Htmlhelper is not getting the model for UmbracoViewPage umbraco 10 by the code below

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyPage>
    

    Seems to me Htmlhelper only works when declaring model class explicitly, which will not work for Umbraco RenderController/View

    @model MyModelClass
    

    Thanks for your reply!

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 22, 2023 @ 17:07
    Huw Reddick
    0

    Mmmm, not sure then, you should be abe to do what you want without a problem

    (example code form one of my views)

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<NoLayout>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    
    @{
        Layout = "SiteMaster.cshtml";
    }
    @using (Html.BeginUmbracoForm<MembershipSurfaceController>("SubmitSigninForm", FormMethod.Post))
    {
         .... form stuff
    }
    
  • May Zhang 11 posts 61 karma points
    Feb 22, 2023 @ 19:50
    May Zhang
    0

    I'm seeing Postback a SurfaceController action ("SubmitSigninForm") within the view. What is the model type for IHtmlHelper when hovering mouse over 'Html.BeginUmbracoForm", is it 'NoLayout'?

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 23, 2023 @ 08:02
    Huw Reddick
    0

    mouse over 'Html.BeginUmbracoForm", is it 'NoLayout'?

    No, it is MvcForm which is what I would expect it to be

  • May Zhang 11 posts 61 karma points
    Feb 23, 2023 @ 18:20
    May Zhang
    0

    Thanks. Your example is a view/form for SurfaceController, mine is MVC controller/view.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 23, 2023 @ 21:22
    Huw Reddick
    0

    The form is for a surface cotroller yes, what are you using the MVC controller for maybe if you posted your code it would be easier to offer some help.

  • May Zhang 11 posts 61 karma points
    Feb 23, 2023 @ 21:41
    May Zhang
    0

    Code is very simple as attached, model is supposed to be "ASICompLogin", but it is actually "dynamic" for HtmlHelper.

    enter image description here

    But razor "Model" is correct type, as attached in this second screenshot.

    enter image description here

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 24, 2023 @ 08:20
    Huw Reddick
    0

    OK, I see now. Is there any reason you are not using

    @using (Html.BeginUmbracoForm("Login", "Account",new{@class="something"},FormMethod.Post)
    

    rather than just Html.BeginForm ?

  • May Zhang 11 posts 61 karma points
    Feb 24, 2023 @ 15:28
    May Zhang
    0

    We have quite some MVC RenderControllers with different views in our Umbraco 8 application. I don't see why they could not be used in 10.

    Regarding SurfaceController post action(BeginUmbracoForm), did you ever encounter the error below?

    "No service for type "MySurfaceController" has been registered"

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Feb 24, 2023 @ 18:24
    Huw Reddick
    100

    yes if using the form in a viewcomponent, there is an existing post discussing it and the solution

Please Sign in or register to post replies

Write your reply to:

Draft