Copied to clipboard

Flag this post as spam?

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


  • Giampaolo Cutroneo 29 posts 159 karma points
    Jul 20, 2023 @ 15:50
    Giampaolo Cutroneo
    0

    SurfaceController and blocklist model data

    I have a page with a blocklist. One of the blocklist items is a form with parameters that vary depending on its position in the blocklist. parameters are properties of the element (textstring, int). The submit of the form is associated with an action of a surface controller. How do I retrieve only the model of the element and not the one of the whole page in the controller?

    Thanks

    Giampaolo

  • Gregory 2 posts 73 karma points
    Jul 22, 2023 @ 04:39
    Gregory
    0

    Based on the information provided, it seems like you have a page with a blocklist, and within the blocklist, there is a form with parameters that vary based on its position. You want to retrieve only the model of the element associated with the form, not the entire page model, in the controller.

    To achieve this, you can use the "BindProperty" attribute in your Surface Controller to selectively bind the model for the specific element Pay My Doctor associated with the form. There's how you can do it:

    Create a model class for the element's properties. For example:

    public class ElementModel { public string TextString { get; set; } public int IntValue { get; set; } }

    In your Surface Controller, define a property with the same model class for the element:

    public class YourSurfaceController : SurfaceController { // Other actions and code

    [BindProperty]
    public ElementModel Element { get; set; }
    
    // Your action method for handling the form submission
    public IActionResult SubmitForm()
    {
        // Access the properties of the specific element only using the "Element" property
        // For example, you can access TextString and IntValue like this:
        string textString = Element.TextString;
        int intValue = Element.IntValue;
    
        // Your processing logic here
    
        return View(); // Or return a Redirect or any other result as needed
    }
    

    }

  • Giampaolo Cutroneo 29 posts 159 karma points
    Jul 24, 2023 @ 08:54
    Giampaolo Cutroneo
    0

    Thank you Gregory, I tried but it doesn't work for me. The controller still uses the page model and not the partial view model. The form is created using BeginUmbracoForm. Maybe it depends on that?

Please Sign in or register to post replies

Write your reply to:

Draft