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.
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?
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.
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.
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!
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.
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?
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.
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?
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
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
Hi,
I assume you're using Data Annotations
and the html helpers - it should render them correctly.
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!
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.
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
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.
is working on a reply...