0

I am using tuples to load multiple models into a view.

The view is as

 @using (Ajax.BeginForm(null, null, ajaxOptions, new { @class = "contact-form" }))

        {
           @Html.AntiForgeryToken()
           <div class="form-group form-group--xs">
              @Html.TextBoxFor(m => m.Item2.Email, new { @class = "form-control input-sm", placeholder = "Your email address..." })
              @Html.ValidationMessageFor(m => m.Item2.Email)
           </div>
           <div class="form-group form-group--xs">
              @Html.TextAreaFor(m => m.Item2.Message, new { @class = "form-control input-sm", placeholder = "Your message...", rows = "4" })
              @Html.ValidationMessageFor(m => m.Item2.Message)
           </div>

           <input type="submit" class="btn btn-primary-inverse btn-sm btn-block" value="Send Your Message" />
        }

The controller is

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
        {
            bool isMessageSent = true;

            if (ModelState.IsValid)
            {
                try
                {
                    await Services.EmailService.SendContactForm(model);
                }
                catch (Exception ex)
                {
                    isMessageSent = false;
                }
            }
            else
            {
                isMessageSent = false;
            }
            return PartialView("_SubmitMessage", isMessageSent);
        }

and the contact form is

 public class ContactForm
    {
        [Required(ErrorMessage = "You must provide your email."), EmailAddress]
        public string Email { get; set; }

        [Required(ErrorMessage = "You must include a message.")]
        public string Message { get; set; }
    }

Finally the partial view is

@model bool

@if (Model)
{
    <div style="color:#c2ff1f">Your message has been sent.</div>
}
else
{
    <div style="color:#f34141">An error occured while sending your message</div>
}

Update: The rendered html for the email textbox is

<input class="form-control input-sm" 
data-val="true" 
data-val-email="The Email field is not a valid e-mail address." 
data-val-required="You must provide your email." 
id="Item2_Email" 
name="Item2.Email" 
placeholder="Your email address..." 
type="text" 
value="">

If i do not enter any text in the email field or the message field, the message is not sent :) but i have no validation errors in the view :( What am i doing wrong here?

4
  • I'm not sure, but I think it would be better if you return a view instead of a partialview. You will get to the screen where it displays that an error occured while sending the email. I think it's enough when you insert "return View();" in the else block of the controller. Commented Apr 1, 2017 at 8:29
  • @Larce Changed to public async Task<ViewResult> and return View(model); but it is also not working as expected Commented Apr 1, 2017 at 8:36
  • What's happening then? Do you still get to the partial view? Commented Apr 1, 2017 at 8:37
  • I mean the validation errors are not displaying, i am not returning the partial view now. I guess something is broken with the tuple ones Commented Apr 1, 2017 at 8:39

3 Answers 3

1

I guess i need a break. I was missing

@Scripts.Render("~/bundles/jqueryval")
Sign up to request clarification or add additional context in comments.

Comments

0

You are returning a partialView that doesn't have the validation error message code.

Try this code:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
        {
            bool isMessageSent = true;

            if (ModelState.IsValid)
            {
                try
                {
                    await Services.EmailService.SendContactForm(model);
                }
                catch (Exception ex)
                {
                    isMessageSent = false;
                }

            }
            else
            {
                isMessageSent = false;
                //Return the view that has validation error message display code.
                return View(model);
            }
            return PartialView("_SubmitMessage", isMessageSent);
        }

5 Comments

Hello, can i return both PartialViewResult and ViewResult from the same controller?
If you are returning actionResult. Yes!
@OrElse the html in your update, is the code generated prior to hitting submit or after hitting submit (returned with Error) ?
@Kumar_Vikas We can't return both PartialViewResult and ViewResult from the same controller.
@ParthRuparelia Why not?
0

For those people like me just add these scripts to your view:

  1. jquery.validate
  2. jquery.validate.unobtrusive

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.