What is Remote Attribute?

Underline Problem:-
Any identity thing is unique example userid / emailid / username so to validate uniqueness we have to compare id with existing data
(most obvious database)

Solution:-
Decorating a model property with this attributes will instruct JQuery validate to make an HTTP request to a given action method
for server-side checking. The result is transmitted back to the client and an error message will be displayed before the form is
submitted

We are taking even number example entered at user-end
Create A View

@model MvcApplication1.Controllers.Category
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@{Html.EnableClientValidation(true);}
@using (Html.BeginForm("Create", "Employee", FormMethod.Post))
{
   @Html.ValidationSummary(false)
   @Html.LabelFor(m => m.PIN)
   @Html.TextBoxFor(m => m.PIN, new { maxlength=1  }) <br />        
   <input type="submit" value="Submit" />
}

Create A Model

public class Category
{
   [Remote("IsEvenNumber", "Employee", ErrorMessage = "Enter Even Number")]
   public int PIN { get; set; }
}

Create A Controller

public class EmployeeController : Controller
{
   [HttpGet]
   public ActionResult create()
   {
      return View();
   }
   public JsonResult IsEvenNumber(int PIN)
   {
      return Json(PIN % 2 == 0, JsonRequestBehavior.AllowGet);
   }
}

I can see AJAX request for validation on onkeyup for each character type in textbox
instead of tab (onBlur)

<script language="javascript" type="text/javascript">
$.validator.setDefaults({
onkeyup: false
})
</script>

What is the user of AdditionalFields attributes?

Use to send multiple values to action for validation

------------------------------------------------
Model
------------------------------------------------
public class Category
{
   [Required]
   [Remote("IsEvenNumber", "Employee", AdditionalFields = "hide_pin", ErrorMessage = "Enter Even Number & Not correct pin")]   
   public int PIN { get; set; }
}

------------------------------------------------
View
------------------------------------------------
@model MvcApplication1.Controllers.Category
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
    $.validator.setDefaults({
        onkeyup: false
    })
</script>

@{Html.EnableClientValidation(true);}
@using (Html.BeginForm("Create", "Employee", FormMethod.Post))
{
    @Html.ValidationSummary(false)

    @Html.LabelFor(m => m.PIN)
    @Html.TextBoxFor(m => m.PIN, new { maxlength = 4 }) <br />

    @Html.Hidden("hide_pin", "10")
    <input type="submit" value="Submit" />
}

------------------------------------------------
Controller
------------------------------------------------
public class EmployeeController : Controller
{
   [HttpGet]
   public ActionResult create()
   {
      return View();
   }

   public JsonResult IsEvenNumber(int PIN, int hide_pin)
   {
      return Json(PIN % 2 == 0 && PIN == hide_pin, JsonRequestBehavior.AllowGet);
   }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s