asp.net-mvc ASP.Net MVC 3 中的远程验证:如何在 Action 方法中使用 AdditionalFields
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4752877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method
提问by beaudetious
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:
我一直在使用新的 ASP.Net MVC 3 RemoteAttribute 向具有单个参数的操作方法发送远程调用。现在我想使用 AdditionalFields 属性传入第二个参数:
[Remote("IsEmailAvailable", "Users", AdditionalFields = "InitialEmail")]
Where IntialEmail is a hidden field in the view. The action looks like so:
其中 IntialEmail 是视图中的隐藏字段。动作看起来像这样:
public JsonResult IsEmailAvailable(
string email,
string InitialEmail)
{
//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.
渲染视图时,填充了隐藏字段,但是当远程触发 Action 方法时,该值为空字符串。
I've seen elsewhere case sensitivity may be an issue, so I've ensured the Action method has the same case for both parameters.
我在其他地方看到过区分大小写可能是一个问题,因此我确保 Action 方法对两个参数具有相同的大小写。
Any other suggestions? This AdditionalFields used to be called Fields.
还有其他建议吗?这个 AdditionalFields 曾经被称为 Fields。
Thanks,
谢谢,
Beaudetious
美丽的
回答by Darin Dimitrov
Strange. It works for me:
奇怪的。这个对我有用:
Model:
模型:
public class MyViewModel
{
[Required]
[Remote("IsEmailAvailable", "Home", AdditionalFields = "InitialEmail")]
public string Email { get; set; }
}
Controller:
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
public ActionResult IsEmailAvailable(string email, string initialEmail)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
View:
看法:
@model AppName.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="[email protected]" />
<input type="submit" value="OK" />
}
IIRC there was some bug in ASP.NET MVC 3 RC2 with this remote validation that was fixed in the RTM.
IIRC 在 ASP.NET MVC 3 RC2 中存在一些错误,此远程验证已在 RTM 中修复。
回答by Christian
Your hidden field must be inside the same form as the field your are validating ( like it is in Darin's example ), otherwise the hidden field's value will not be sent as parameter to the validation action method "public ActionResult IsEmailAvailable(string email, string initialEmail)"
您的隐藏字段必须与您正在验证的字段位于同一表单内(就像在 Darin 的示例中一样),否则隐藏字段的值将不会作为参数发送到验证操作方法“public ActionResult IsEmailAvailable(string email, string initialEmail)” )”
回答by Ericyu67
function IsEmailAvailable(string email, string initialEmail) param email should as Email which exactly same as Property Email.
function IsEmailAvailable(string email, string initialEmail) param email 应该是与属性电子邮件完全相同的电子邮件。