Copied to clipboard

Flag this post as spam?

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


  • Keith 11 posts 90 karma points
    Apr 22, 2024 @ 14:26
    Keith
    0

    How to pass complex model to SurfaceAction

    Hi

    Umbraco 13 and in a view I have a hyperlink. I'm trying to pass in some data from this view to a controller (surface controller)

    The model is

    Public Customer MCustomer
    {
       public string FreeText {get; set;}
       public CustomModel Custom {get; set;}
    }
    

    In my view I add

    <a href="@Url.SurfaceAction("myMethod", "CustomSurface", new MCustomer{CustomModel = new CustomModel {propOne="hello"}, FreeText= "this is free text"})"> Test</a>
    

    In this scenario the free text content comes through but the custom model doesn't? Why and how can I resolve this?

    Thanks

  • Venkat 6 posts 97 karma points
    Apr 23, 2024 @ 12:57
    Venkat
    0

    Hey Keith, update your model class like below

    public class MyViewModel
    {
        public string FreeText { get; set; }
        public CustomModel Custom { get; set; }
    }
    

    and bind your values to the model object like below in the razor view

    @{
        var myModel = new MyViewModel
        {
            FreeText = "this is free text",
            Custom = new CustomModel { propOne = "hello" }
        };
    }
    

    and finally call your anchor tag element with method name, controller name and data object model

    <a href="@Url.SurfaceAction("myMethod", "CustomSurface", myModel)">Test</a>
    
Please Sign in or register to post replies

Write your reply to:

Draft