Tuesday, November 22, 2011

additional parameters to the Json remote validtation

 

 

I have already write simple Example for Remote Attribute but I think i need to add little more in this but for basic working of remote attribute please follow my previous article where I have explain what’s remote attributes and how it will work. from given link Remote Attribute

  In Remote Validation attribute if some time we have case we need to check user input on basis of other parameter. like on registration form of some website needs to allowed user from between 18-32 age so here conditional check means range check. let me explain how we can achieve this using remote attributes. in remote attributes have property to pass AdditionalFields here we can pass as many fields as we want. suppose we want to pass one filed we need to just given value. if we need to pass more than one fields we need to given fields in coma separated like “Field1,Field2”

let check using Example

 

Here is Very Simple Model

// Comment

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Model
{
    public class UserInformation
    {
        [Required]
        [System.Web.Mvc.Remote("IsUserNameExits", "Validation")]         
        public string Name { get; set; }

        [Required] 
        public string City { get; set; }
        public string Account { get; set; }

        [Required]
        [System.Web.Mvc.Remote("AgeAllowed", "Validation", AdditionalFields = "Min,Max")]    
        public Int32 AllowedAge { get; set; }
       
    }
}

 

now look at AgeAllowed where I have given conditional formatting. I want to make age required between Min and max value. now look at my View where

I have display very simple view

// Comment

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "signupform" }))
{
  <div>
  Temp: @TempData["TestTemp"]
  ViewData: @ViewData["TestTemp"]
  </div>  
    
    
    @Html.ValidationSummary(true)
    <fieldset> <legend>UserInformation</legend> <div class="editor-label">@Html.LabelFor(model => model.Name) </div> <div class="editor-field">@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div class="editor-label">@Html.LabelFor(model => model.City) </div> <div class="editor-field">@Html.EditorFor(model => model.City) @Html.ValidationMessageFor(model => model.City) </div> <div class="editor-field">@Html.DropDownList("dplist",(SelectList) ViewBag.DpList) @Html.TextBox("txtbox") </div> <div class="editor-label">@Html.ValidationMessageFor(model => model.AllowedAge) </div> <div class="editor-field">@Html.EditorFor(model => model.AllowedAge) <input value="18" type="hidden" name="Min"> <input value="32" type="hidden" name="Max"> </div> <p> <input value="Save" type="submit"> <input onclick="javascript:AjaxSave();" value="AjaxSave" type="button"> </p> </fieldset>
}

Here I have taken min and max as hidden fields. if your project required to get value from database you can define value in model and get value from database.  

now look at one Controllers I have create for Validation

// Comment


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class ValidationController : Controller
    {
        //
        // GET: /Validation/

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult IsUserNameExits(string Name)
        {
            
            if (Name == "Bhavik")
            {
               return Json("Sorry UserName Already Taken", JsonRequestBehavior.AllowGet);    

            }
            else
            {               
                return Json(true, JsonRequestBehavior.AllowGet);    
            }

            
        }
        //
        public JsonResult AgeAllowed(Int32 AllowedAge, Int32 Min, Int32 Max)
        {
            //
            if (AllowedAge > Max || AllowedAge < Min)
            {
                //
                return Json("Sorry Age requirted beetween " + Min.ToString() + "-" + Max.ToString(), JsonRequestBehavior.AllowGet);    
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);    
            }
        }

    }
}

 

 

now run and check

 

Here is output age allowed between 18-32 age.


2 comments: