Copied to clipboard

Flag this post as spam?

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


  • Erma Isabel 20 posts 91 karma points
    Oct 04, 2013 @ 13:04
    Erma Isabel
    0

    Server side validation with umbraco mvc

    Hello All,

    In my umbraco mvc project, I am trying to validate form with serverside validation.

    Controller code:

        public ActionResult BookRequest(BookRequest model)
        {
                if (ModelState.IsValid)
                {
                   db.BookRequests.Add(model);
                   db.SaveChanges();  
                }
                return View();
        }
    

    Model is,

     public class BookRequest
    {
        public int Id { get; set; }
    
        [Required(ErrorMessage = "Please enter your name")]
        public string Name{ get; set; }
    
        [Required(ErrorMessage = "Please enter your email")]
        public string Email { get; set; }      
    }
    

    Umbraco page

      @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
      @{
           Layout = "Master.cshtml";
       }
    
        <div class="maindiv">
             @Umbraco.RenderMacro("Headmenu",new ViewDataDictionary{})
        </div>
    

    Partial View

     @model MvcApplication2.Models.BookRequest
      <script src="~/Scripts/jquery.validate.min.js"></script>
      <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    
      @using (Html.BeginUmbracoForm<MvcApplication2.Controllers.BookRequestController>("BookRequest"))
      {
         @Html.ValidationSummary(true)
    
          @Html.TextBoxFor(model => model.Name,new { @class = "input",@placeholder="Name"})
          @Html.ValidationMessageFor(model => model.Name)       
    
          @Html.TextBoxFor(model => model.ContactName,new { @class = "input",@placeholder="Email"})
          @Html.ValidationMessageFor(model => model.Email)
          <input type="submit" class="button" value="Send Enquiry" />
    
      }
    

    Saving to database works fine. However, Validation is not showing up. For that, it needs to return model from controller

    return View(model);
    

    when i add above code following error occurs,

     The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'MvcApplication2.Models.BookRequest'.
    

    How can i solve this? Please help,

    Thanks

  • Rich Green 2246 posts 4008 karma points
    Oct 04, 2013 @ 15:49
Please Sign in or register to post replies

Write your reply to:

Draft