Ive got an umbraco page which is using a childaction partial to render a form. This is working fine but the model passed it always empty. Can someone help
Here is what i have
Umbraco Page:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = "Master.cshtml";
}
@Html.Partial("~/Views/Apply/Index.cshtml", new ApplyViewModel())
[ChildActionOnly]
public ActionResult Index()
{
return PartialView();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HandleApplyForm(ApplyViewModel model)
{
//Check if the dat posted is valid (All required's & email set in email field)
if (!ModelState.IsValid)
{
//Not valid - so lets return the user back to the view with the data they entered still prepopulated
TempData.Add("MyModel", model);
return RedirectToCurrentUmbracoPage();
}
ViewModel:
public class ApplyViewModel : RenderModel
{
public ApplyViewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent, UmbracoContext.Current.PublishedContentRequest.Culture)
{
}
[Required]
public string Name;
public string Email;
public string Telephone;
[Required]
public string Vacancy;
[Required] public string Message;
public string Url;
public HttpPostedFileBase CV { get; set; }
}
The model properties are always null (apart from the CV file upload but assuming it gets this from the request object). Can someone help me please? Been tearing my hair out with this since 6am this morning
The site is for a recruitment agency. The initial page is populated with data from Umbraco, i have created it from a document type and created content from the umbraco backend (has then details of the job, decription, salary etc)
I am trying to render the partial view of the apply form to the bottom of these pages. Is this where I am going wrong? WOuld i be better directing to a new page to apply or can i do it from the same page?
this will return to your page and present the form with the Modelstate intact, eg with validation errors for the user to correct.
returning 'RedirectToCurrentUmbracoPage()' is generally used when the form is submitted correctly, has no errors, you've sent an email or inserted the form details into a db and you want to clear the modelstate and return the user back to the page with the form reset - perhaps adding a message to the TempData to say thanks...
but no need to add the model to the TempData dictionary, returning CurrentUmbracoPage will persist the model for you.
Thanks for the reply. I'll implement as you say thanks for that, however the model is always null when i hit the surface controller (apart from the CV file that is uploaded). Any ideas why that might be?
I think your ApplyViewModel shouldn't inherit RenderModel
It should just be a POCO class. containing the properties of the form you are posting.
Your Surface Controller action should receive them, you process the application then redirect the user back to the page - presumalbly you are checking in the rendering of the page whether the logged in user has already applied, so after the redirect, this logic will click in, and you won't display the form again, but maybe add a message to ViewBag to temporarily say 'thanks for applying'
There is another technique in Umbraco called Route Hijacking, where you hijack a request for a page, and run it through your own RenderMvcController, in this context you can create a rich model to use in your templates, and your model would need to inherit RenderModel.
I've removed the inheritance now, id seen a few examples of this and saw some had it but its now removed.
Can someone link me to a good example or guide of how this should work please? I started from scratch and re-defined a very basic model to see if I could get this working and I had the same problem (model properties are always null regardless of what was posted).
If anyone has any other ideas or pointers as to what might be causing this i'd love to hear them!
I worked this out (when I say worked out stumbled across it when I saw some posts about MVC Model binding failing) and forgot to post. Thanks for the help though
No problem, sorry I didn't spot it earlier! - I built it out in visual studio to see what was going wrong, and as I typed the properties... I think the recruitment scenario is a good example for a surface controller tutorial though.
SurfaceController empty model on post
Hi
Ive got an umbraco page which is using a childaction partial to render a form. This is working fine but the model passed it always empty. Can someone help
Here is what i have
Umbraco Page:
Partial view:
Controller:
ViewModel:
The model properties are always null (apart from the CV file upload but assuming it gets this from the request object). Can someone help me please? Been tearing my hair out with this since 6am this morning
To add a bit more context
The site is for a recruitment agency. The initial page is populated with data from Umbraco, i have created it from a document type and created content from the umbraco backend (has then details of the job, decription, salary etc)
I am trying to render the partial view of the apply form to the bottom of these pages. Is this where I am going wrong? WOuld i be better directing to a new page to apply or can i do it from the same page?
Hi iand123
When you have a validation error...
return CurrentUmbracoPage();
this will return to your page and present the form with the Modelstate intact, eg with validation errors for the user to correct.
returning 'RedirectToCurrentUmbracoPage()' is generally used when the form is submitted correctly, has no errors, you've sent an email or inserted the form details into a db and you want to clear the modelstate and return the user back to the page with the form reset - perhaps adding a message to the TempData to say thanks...
but no need to add the model to the TempData dictionary, returning CurrentUmbracoPage will persist the model for you.
regards
Marc
Hi Marc
Thanks for the reply. I'll implement as you say thanks for that, however the model is always null when i hit the surface controller (apart from the CV file that is uploaded). Any ideas why that might be?
Hi iand123
I think your ApplyViewModel shouldn't inherit RenderModel
It should just be a POCO class. containing the properties of the form you are posting.
Your Surface Controller action should receive them, you process the application then redirect the user back to the page - presumalbly you are checking in the rendering of the page whether the logged in user has already applied, so after the redirect, this logic will click in, and you won't display the form again, but maybe add a message to ViewBag to temporarily say 'thanks for applying'
There is another technique in Umbraco called Route Hijacking, where you hijack a request for a page, and run it through your own RenderMvcController, in this context you can create a rich model to use in your templates, and your model would need to inherit RenderModel.
But to just handle a form post it doesn't.
If that makes sense!
regards
Marc
Hi Marc
I've removed the inheritance now, id seen a few examples of this and saw some had it but its now removed.
Can someone link me to a good example or guide of how this should work please? I started from scratch and re-defined a very basic model to see if I could get this working and I had the same problem (model properties are always null regardless of what was posted).
If anyone has any other ideas or pointers as to what might be causing this i'd love to hear them!
Hi iand123
Think the problem likes in your model, you don't have getters and setters defined for your properties:
Try:
it should allow the model binding to work.
(also you don't have a field for vacancy on the form,,, hidden field?)
regards
Marc
I worked this out (when I say worked out stumbled across it when I saw some posts about MVC Model binding failing) and forgot to post. Thanks for the help though
No problem, sorry I didn't spot it earlier! - I built it out in visual studio to see what was going wrong, and as I typed the properties... I think the recruitment scenario is a good example for a surface controller tutorial though.
Hope the rest of it goes well.
regards
Marc
Yeah it's a good idea; I'll try and get a blog up about it
is working on a reply...