CSharp · Mvc
using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.ComponentModel;
using System.Linq;
using System.Web;
namespace HelloWorldMvcApp
{
public class Model
{
[DisplayName("Email address")]
[Required]
public string Email { get; set; }
[DisplayName("User ID")]
[Remote("Validate", "Home", HttpMethod = "Post", AdditionalFields = "Email", ErrorMessage = "Email address and User ID should not be same")]
[Required]
public string UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
@model HelloWorldMvcApp.Model
@{
Layout = null;
}
@{
ViewBag.Title = "Index";
}
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
{
<div class="form-group">
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Email)
<br />
@Html.LabelFor(m => m.UserID)
@Html.TextBoxFor(model => model.UserID, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserID)
<br />
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { @class = "form-control" })
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(model => model.LastName, new { @class = "form-control" })
</div>
<button type="submit" class="btn btn-success submit">submit</button>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View(new Model());
}
[HttpPost]
public void FormSubmit(Model modl)
{
if (ModelState.IsValid)
{
Response.Write("OK");
}
}
[HttpPost]
public ActionResult Validate(string UserID, string Email)
{
return Json(UserID == Email ? false : true);
}
}
}