I've built my first SurfaceController as per the excellent video chapter.
The contact form works fine but, before I set up client validation (which works also) or if I disable client validation, server side validation ends with an error when Submit is clicked only if one or more of the form's fields are empty, as follows:
1. Open the page with the Contact Form.
2. Leave one of more fields empty.
3. Click Submit.
4. In debug, the controller recognises that the model is invalid: if (!ModelState.IsValid)
5. The page with the contact form renders but the macros in the page (the contact form macro and the navigation macro) do not load with the following messages embedded in the page:
Error loading Partial View script (file: ~/Views/MacroPartials/Navigation.cshtml)
and
Error loading Partial View script (file: ~/Views/MacroPartials/ContactForm.cshtml)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ParishMaster.Models
{
public class ContactFormViewModel
{
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
}
I'm on my mobile here so bear with me. I can see your problem. The action result that loads your form creates a new model each time. You need to duplicate the method using the same name. Above the first one put [HttpGet] this one can be the one which uses a new model.
Then above the duplicate one put [HttpPost] and inside the brackets get it to expect the model ie.
[HttpPost]
Public actionresult loadform(modelclassname model)
Return partialview("partialviewname", model);
The key here is that on the post back you are looking for the model and you use that model again instead of creating a new one.
Server side validation error
I've built my first SurfaceController as per the excellent video chapter.
The contact form works fine but, before I set up client validation (which works also) or if I disable client validation, server side validation ends with an error when Submit is clicked only if one or more of the form's fields are empty, as follows: 1. Open the page with the Contact Form. 2. Leave one of more fields empty. 3. Click Submit. 4. In debug, the controller recognises that the model is invalid: if (!ModelState.IsValid) 5. The page with the contact form renders but the macros in the page (the contact form macro and the navigation macro) do not load with the following messages embedded in the page: Error loading Partial View script (file: ~/Views/MacroPartials/Navigation.cshtml) and Error loading Partial View script (file: ~/Views/MacroPartials/ContactForm.cshtml)
What am I doing wrong?
Your help would be much appreciated.
Thanking you in anticipation.
Roger
Can you paste your code?
Thanks, Ifrahim. Herewith the code:
Partial macro:
Model:
View:
Controller:
Did you try to use RedirectToCurrentUmbracoPage ?
If I use RedirectToCurrentUmbracoPage, the page reloads without errors but the "required field" messages does not display alongside the text boxes.
Hi Roger,
Did you find the solution?? I also have a same issue.
I'm on my mobile here so bear with me. I can see your problem. The action result that loads your form creates a new model each time. You need to duplicate the method using the same name. Above the first one put [HttpGet] this one can be the one which uses a new model.
Then above the duplicate one put [HttpPost] and inside the brackets get it to expect the model ie.
[HttpPost] Public actionresult loadform(modelclassname model) Return partialview("partialviewname", model);
The key here is that on the post back you are looking for the model and you use that model again instead of creating a new one.
is working on a reply...