Copied to clipboard

Flag this post as spam?

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


  • Sabin Regmi 2 posts 71 karma points
    May 30, 2016 @ 08:37
    Sabin Regmi
    0

    Server side form validation in umbraco+mvc

    Hi,

    I am trying to validate the form on client side(using jQuery) as well as on server side. It works fine on client side but server side validation is not working.

    My model is:

    public class CustomModel
    
     {
    
        public int Id { get; set; }
    
        [Required]
        public string Type { get; set; }
    
        [Required]
        public string Amount { get; set; }
    
        [Required]
        public string Time { get; set; }
    
        public string Gender { get; set; }
        [Required]
    
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
    
     }
    

    Controller:

    public class CustomSurfaceController : SurfaceController
      {
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Index(CustomModel model)
        {
            var db = ApplicationContext.Current.DatabaseContext.Database;
    
            if (ModelState.IsValid)
            {
              //code
            }
      }
    }
    

    View Page:

     @using (Html.BeginUmbracoForm("Index", "CustomSurface", null, new { id = "defaultForm", @class = "form-horizontal" }))
                    {
                        @Html.AntiForgeryToken()
                         <label>@Html.RadioButtonFor(x => Model.Type, @oneOff, new { @class = "regular-radio" })<span class="radiobtn"></span>@oneOff</label>
                         <label>@Html.RadioButtonFor(x => Model.Type, "Periodiek", new { @checked = true, @class = "regular-radio" })<span class="radiobtn"></span>@periodic</label>
                          @Html.TextBoxFor(x => Model.Amount, new { id = "txtAmt", @class = "form-control", @placeholder = "Amount" })        
                          @Html.TextBoxFor(x=>Model.Time)
                          <label>@Html.RadioButtonFor(x => Model.Gender, "Mr", new { @checked = true, @class = "regular-radio" })<span class="radiobtn"></span>Mr</label>
                          <label>@Html.RadioButtonFor(x => Model.Gender, "Mrs", new { @class = "regular-radio" })<span class="radiobtn"></span>Mrs</label>
                          @Html.TextBoxFor(x => Model.FirstName, new { @placeholder = "First Name" + "*", @class = "form-control" })
                          @Html.TextBoxFor(x => Model.LastName, new { @placeholder = "Last Name" + "*", @class = "form-control" })
                          <button type="submit" name="submitButton">Submit</button>
    
                      }
    

    What should I have to do? So, that server side validation also works.

    Thanks in advance.

  • Bijesh Tank 192 posts 420 karma points
    May 31, 2016 @ 09:34
    Bijesh Tank
    0

    Hi Sabin,

    You need to add validation extensions to your view. For example:

    @Html.TextBoxFor(x => Model.FirstName, new { @placeholder = "First Name" + "*", @class = "form-control" })
    @Html.ValidationMessageFor(m => Model.FirstName)
    

    You should also consider adding JavaScript unobtrusive settings to your application as well. Take a look at this Nuget package

Please Sign in or register to post replies

Write your reply to:

Draft