32

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:

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.

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

Thanks,

Beaudetious

beaudetious
  • 2,354
  • 3
  • 36
  • 60
  • Can you add the markup of your view? The additional field needs to match one of the fields in your view. Do you get a value for email but an empty string for InitialEmail? To investigate it is helpful to debug with firebug and you can see the request that is sent back to the server. It should have a querystring for the fields that your action is expecting. Something like ?email=blah?InitialEmail=blah... – Joe Cartano Jan 22 '11 at 01:21
  • The answer to another one of my questions resolved this one too: http://stackoverflow.com/questions/4696276/client-side-validation-not-firing-for-compareattribute-dataannotation So how should I mark this question answered? – beaudetious Jan 31 '11 at 17:50

3 Answers3

60

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="foo@bar.com" />
    <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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 6
    Darin: You and your "it works for me" is going to be the death of me one day. ;) – beaudetious Jan 21 '11 at 18:58
  • Seriously though, print the value of InitialEmail in the IsEmailAvailable method to the Output window. You'll see it's blank. I am able to fire the remote method, but the additional parameter has no value. – beaudetious Jan 21 '11 at 18:59
  • @beaudetious, I did it and it wasn't blank. Also I looked with FireBug at the request that was sent. I could see the value as well :-) Wait, I will send you my sample project. Just to finish my beer. – Darin Dimitrov Jan 21 '11 at 19:04
  • Hey, any differences when setting this up in MVC 4? I can't seem to get it to work – Pittfall May 10 '12 at 13:50
  • 1
    I am not aware of any differences. – Darin Dimitrov May 10 '12 at 13:52
  • 2
    I found it. you have to set this attribute in the controller `[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]` – Pittfall May 10 '12 at 13:56
  • or take away `[Authorize]` which is there by default – Pittfall May 10 '12 at 13:58
  • Also Dimitrov, you seem to know quite a bit about this stuff, what is the best way to set up a timer so that it does not try and validate it right away but maybe wait about 3 seconds. I would like for it to validate before tab out of textbox too – Pittfall May 10 '12 at 14:01
  • 1
    @Pittfall Override the RemoteValidation implementation :P – Elisabeth Aug 10 '12 at 20:44
  • hi @DarinDimitrov why "ActionResult" and not "JSONResult" ?? we are returning JSON.. – SurajS Jul 28 '15 at 09:51
2

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

Christian
  • 420
  • 1
  • 8
  • 15
0

function IsEmailAvailable(string email, string initialEmail) param email should as Email which exactly same as Property Email.

Ericyu67
  • 105
  • 1
  • 4
  • 11