mvc 远程验证 引用“http://stackoverflow.com/questions/4752877/remote-validation-in-asp-net-mvc-3-how-to-use-additionalfields-in-action-method”
I've been using the new ASP.Net MVC 3 RemoteAttribute to send a remote call to an action method that had a single parameter. Now I want to pass in a second parameter using the AdditionalFields property:
[Remote("IsEmailAvailable","Users",AdditionalFields="InitialEmail")]
Where IntialEmail is a hidden field in the view. The action looks like so:
publicJsonResultIsEmailAvailable(
string email,
stringInitialEmail)
{
//etc.
}
When the view is rendered, the hidden field is populated, but when the Action method is triggered remotely, the value is an empty string.
I've seen elsewhere case sensitivity may be an issue, so I've ensured the Action method has the same case for both parameters.
Any other suggestions? This AdditionalFields used to be called Fields.
Thanks,
Beaudetious
Strange. It works for me:
Model:
publicclassMyViewModel
{
[Required]
[Remote("IsEmailAvailable","Home",AdditionalFields="InitialEmail")]
publicstringEmail{get;set;}
}
Controller:
publicclassHomeController:Controller
{
publicActionResultIndex()
{
returnView(newMyViewModel());
}
[HttpPost]
publicActionResultIndex(MyViewModel model)
{
returnView(model);
}
publicActionResultIsEmailAvailable(string email,string initialEmail)
{
returnJson(false,JsonRequestBehavior.AllowGet);
}
}
View:
@modelAppName.Models.MyViewModel
@{
ViewBag.Title="Home Page";
}
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using(Html.BeginForm())
{
@Html.TextBoxFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email)
<input type="hidden" name="InitialEmail" value="foo@bar.com"/>
<input type="submit" value="OK"/>
}

浙公网安备 33010602011771号