在ASP.Net MVC 3远程验证:如何使用AdditionalFields行动方法 [英] Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

查看:457
本文介绍了在ASP.Net MVC 3远程验证:如何使用AdditionalFields行动方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用新的ASP.Net MVC 3 RemoteAttribute发送到了一个单一的参数的操作方法远程调用。现在,我想用AdditionalFields属性在第二个参数来传递:

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")]

在哪里IntialEmail是在视图中隐藏字段。动作看起来就像这样:

Where IntialEmail is a hidden field in the view. The action looks like so:

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.

我见过其他地方的大小写可能是一个问题,所以我已经保证了操作方法有两个参数不变的情况下。

I've seen elsewhere case sensitivity may be an issue, so I've ensured the Action method has the same case for both parameters.

任何其他建议?使用该AdditionalFields被称为字段。

Any other suggestions? This AdditionalFields used to be called Fields.

谢谢,

Beaudetious

Beaudetious

推荐答案

奇怪。它为我的作品:

型号:

public class MyViewModel
{
    [Required]
    [Remote("IsEmailAvailable", "Home", AdditionalFields = "InitialEmail")]
    public string Email { get; set; }
}

控制器:

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);
    }
}

查看:

@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="foo@bar.com" />
    <input type="submit" value="OK" />
}

IIRC有在ASP.NET MVC 3 RC2与固定在RTM这个偏远验证了一些bug。

IIRC there was some bug in ASP.NET MVC 3 RC2 with this remote validation that was fixed in the RTM.

这篇关于在ASP.Net MVC 3远程验证:如何使用AdditionalFields行动方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆