>>>Hi Viv,
>>>
>>>This doesn't seem to work for me. I am getting null being sent to the controller's method instead of the value I typed.
>>>
>>>Here is what I see when I inspect this element in Opera:
>>>
>>>
>>><div class="editor-field" style="background-color: rgb(242, 250, 237);">
>>><input class="text-box single-line valid" data-bind="value: Name" data-val="true"
>>>data-val-remote="Client Name already exists. Please enter a different Client Name."
>>>data-val-remote-additionalfields="*.Name" data-val-remote-type="POST"
>>>data-val-remote-url="/Client/doesClientNameExist"
>>>data-val-required="The Client Name field is required." id="Name" name="Client.Name" type="text" value=""/>ev
>>><span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"/>
>>>
>>>
>>>Could the problem be that the Id is "Name" but the name is "Client.Name"
>>>
>>>?
>>Don't know why id is "Client.Name" but make sure that the parameter name in the validation method exactly matches the control id. i.e.:
public JsonResult doesClientNameExist(string Name)
>> {
>> }
If it doesn't you will get null for sure....
>
>I have another question. I found that when I open the Edit form, the validation fire right away.
>
>I am not sure why does it happen and how to prevent it. Also, for the edit I only want to check for uniqueness if there is another id with the same client no.
>
>So, this is what I tried - in the Edit method (Get version) of the controller I set ViewBag.ClientID = id; and I changed my methods to be
>
>
>[HttpPost]
> public JsonResult doesClientNoExist(Int16 Number)
> {
> bool result = false;
>
> if (ViewBag.ClientId !=null)
>
> {
> int id = ViewBag.ClientId;
> result= Db.Clients.Any(c => c.Number == Number && c.Id !=id);
> }
> else
> result= Db.Clients.Any(c => c.Number == Number) ;
>
> return Json((!result), JsonRequestBehavior.AllowGet);
> }
>
>
>However, ViewBag.ClientId returns null and so immediately after I fire that form it comes up with the Client No aleady exists message.
>
>Do you see how can I solve this problem?
>
>Thanks in advance.
Bit hard to see where Number / ClientId comes from.
But if, for example, ClientId is a control on the form then you can use the 'AdditionalFields' property of the Remote attribute when validating the name. Something like:
[Remote("doesClientNameExist", "Account",AdditionalFields = "ClientId")]
and
public JsonResult doesClientNameExist(string UserName, string ClientId)
{
}
then you'll have both properties available. Don't know if this is what you were looking for tho :-{
Also, without looking more, I've no suggestions as to why the validation fires on keypress......