Copied to clipboard

Flag this post as spam?

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


  • Gordon Smith 21 posts 103 karma points
    Aug 07, 2020 @ 16:43
    Gordon Smith
    0

    How to update Umbraco content in controller?

    I'm building a site that has members who can log in and add/update their own business listings.

    "Business" is a document type in Umbraco.

    I am stumped when it comes to creating a view to allow a logged-in member to edit their own business listing.

    I cannot understand how to access to Umbraco-created model for the Business listing in a form postback (preferably Ajax postback).

    The "get" action works OK, and populates my form fine.

    But the "post" action - I don't know how to post the Business model back to the action, or post an Id so I can get the Content in the controller. Even though I don't know how to update the content with the form data.

    I'm looking for some simple example online for this. Do you know of any?

    I'm new to Umbraco and MVC so working out whether my problem is Umbraco or MVC-related is really difficult.

    Thanks to everybody in advance.

  • Gordon Smith 21 posts 103 karma points
    Aug 10, 2020 @ 18:46
    Gordon Smith
    0

    I have made some progress but I am not there yet.

    I have managed to build a View that's bound to an Umbraco document type.

    However the form validation is not running and ModelState.IsValid therefore always returns true.

    I narrowed this down to the fact that the ModelsBuilder (I am using AppData) does not put data annotations in the *.generated.cs files so the validation attributes are not output on the controls in the View.

    I tried manually putting data annotations in my Business.generated.cs file. This worked when compiled but of course is overwritten every time the project is built.

    So how do you tell Umbraco to add the data annotatations or otherwise how do you leverage MVC validation for Umbraco content type?

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Aug 11, 2020 @ 10:10
    Steve Morgan
    0

    Hi Gordon.

    Very loosely - you should be using a Surface Controller. These are best for form posts - even Ajax form posts though there is a bit more fiddling as they take care of Umbraco context and form posts.

    Then when you have that working you'll need to use the ContentService to update Umbraco content.

    You'll likely need to embed the user's business node ID in some form into a hidden variable in the form. I'd suggests you use the guids for the business page nodes rather than putting Umbraco node IDs into form posts (for security).

    Hopefully the above will get you on the right lines.

    Steve

  • Gordon Smith 21 posts 103 karma points
    Aug 11, 2020 @ 10:20
    Gordon Smith
    0

    Hi Steve

    Thanks for replying. I have advanced my code and have it running as per your suggestions (SurfaceController + ContentService + Id hidden in form).

    My roadblock now is that the fields which are mandatory in Umbraco don't generate the necessary jQuery validation attributes such as data-val="true" and data-val-required="xxx" on the form controls.

    Therefore client-side validation is not running and ModelState.IsValid() is always true.

    Any ideas on this? When this is done I will post the code.

    Thanks, Gordon

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Aug 12, 2020 @ 13:45
    Steve Morgan
    0

    Hi,

    I assume you're using Data Annotations

      [Required(ErrorMessage = "Please enter your name.")]
    

    and the html helpers - it should render them correctly.

     @Html.TextBoxFor(m => m.Name, new { placeholder = "Name", @class = "form-control" })
    

    You'll have to post an example - though I hate unobtrusive validation and avoid it. It causes more issues than it solves. I find it's usually a mistake in the order you add the script tags for jquery and the unobtrusive and how you fire the ajax call.

    Then you add Google recaptcha to the mix.... ARGGGHHH!

  • Gordon Smith 21 posts 103 karma points
    Aug 13, 2020 @ 12:53
    Gordon Smith
    0

    Hi Steve.

    The problem is that the expected data annotations are not being generated by the Models Builder for my Umbraco document types.

    For example, if I set a text field as mandatory in Umbraco, then I would expect the Models Builder to inject the [Required] data annotation into the Model.generated.cs file.

    This in turn would generate the necessary data-val and data-val-required tags in the output HTML when compiled.

    In my case the Models Builder is not adding data annotations for my Umbraco document types.

    Does the Models Builder not add the required data annotations automatically? I would expect it to, but in my case, it does not.

    Thanks, Gordon.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Aug 14, 2020 @ 08:31
    Steve Morgan
    0

    Hi Gordon,

    No - the required flag in Umbraco is simply for the back office.

    It's not great practice to use the models generated by the models builder in your forms. Best to create separate models and then map these to your Umbraco doc types. That way you can merrily decorate these with your data attributes and validation.

    These models would be cut down versions of what you have - it might have what feels like additional overhead but you wouldn't want an editor to add a doc type property and break your front end form posts?

    Steve

  • Gordon Smith 21 posts 103 karma points
    Aug 14, 2020 @ 10:19
    Gordon Smith
    0

    OK, thanks.

    This has helped my understanding of how Umbraco works.

    I also see the logic of separating the model from the front-end validation. It's more work, but never mind.

    Thanks for your help. I can move this forward now.

    Gordon.

Please Sign in or register to post replies

Write your reply to:

Draft