0

I need to make my own validate function, and i found this: http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property

I was trying to use Remote Attribute, however chrome doesn't send any information to my JsonResult Method and i don't know why. Console is empty when i switching between fields: enter image description here

I found a similar problem Remote Validation for LIST of MODELs But still it doesn't work.

Scripts are in BundleConfig, i see them in the source page.

jquery.validate.js:

   remote: function( value, element, param ) {
    //...
                //data[element.name] = value;
                data[element.name.substr(element.name.lastIndexOf(".") + 1)] = value;
    //...
    });

jquery.validate.unobtrusive:

adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
    var value = {
        url: options.params.url,
        type: options.params.type || "GET",
        data: {}
    },
        prefix = getModelPrefix(options.element.name);

    $.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {

        var paramName = fieldName.substr(fieldName.lastIndexOf(".") + 1);

        var actualFieldName = appendModelPrefix(fieldName, prefix)
        value.data[paramName] = function () {
            return $(options.form).find(":input").filter("[name='" + escapeAttributeValue(actualFieldName) + "']").val();
        };
    });

    setValidationValues(options, "remote", value);
});

My ViewModel:

[Remote("Divisibility", "Account", ErrorMessage = "Value is incorrect. (REMOTE)")]
public int Amount { get; set; }

My View:

@model PROJECT.WebUI.ViewModels.DataItemVm
//... the rest of table    
@using (Html.BeginForm())
{
  @Html.ValidationSummary("", new { @class = "text-danger" })
    for (var i = 0; i < Model.FirstSetList.Count; i++)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => model.FirstSetList[i].Name)
                @Html.HiddenFor(model => model.FirstSetList[i].Name)
            </td>
            <td>
                @Html.DisplayFor(model => model.FirstSetList[i].Pack)
                @Html.HiddenFor(model => model.FirstSetList[i].Pack)
            </td>
            <td>
                @Html.TextBoxFor(model => model.FirstSetList[i].Amount)
                @Html.HiddenFor(model => model.FirstSetList[i].Amount)
            </td>
        </tr>
    }
    <input type="submit" value="Confirm" class="btn btn-success" />
}

My Controller:

public JsonResult Divisibility(int Amount)
{
    var value = User.Identity.GetUserId().Where(x => x.Equals("qqqqq"));
    //I know that this condition does not make sense, but this was only for test.
    //Anyway like i said, chrome doesn't send anything to this method.

    return Json(value == null, JsonRequestBehavior.AllowGet);
}

Actual html generated for one input for an Amount property

<td>
<input data-val="true" data-val-number="The field Amount must be a number." data-val-remote="Value is incorrect (REMOTE)." data-val-remote-additionalfields="*.Amount" data-val-remote-url="/Account/Divisibility" data-val-required="The Amount field is required." id="FirstSetList_0__Amount" name="FirstSetList[0].Amount" type="text" value="0">
<input id="FirstSetList_0__Amount" name="FirstSetList[0].Amount" type="hidden" value="0">
</td>
Community
  • 1
  • 1
DiPix
  • 5,755
  • 15
  • 61
  • 108
  • You don't have any `ValidationMessageFor()` associated with the property so any error would not display anyway :). Show the actual html generated for one of your inputs for an `Amount` property - does it include the `data-val-*` attributes? –  Mar 23 '16 at 10:24
  • Good point, but when i put breakpoint in `JsonResult Divisibility` method, application never reached the breakpoint. However I added `@Html.ValidationSummary("", new { @class = "text-danger" })`. Amount property has data-val-*, i edited post above. – DiPix Mar 23 '16 at 11:12
  • The code you have shown will work fine. If its not, then their is something else in your code causing an issue, most likely some other scripts. –  Mar 23 '16 at 11:19
  • Any idea whould should I do? – DiPix Mar 23 '16 at 11:26
  • Do you have any other scripts except for `jquery`, `jquery.validate` and `jquery.validate.unobtrusive`? If so comment them all out and test it. –  Mar 23 '16 at 11:28
  • Well only this, so it shouldn't be a problem: [all script at view](http://i.imgur.com/KaY2lGy.png) – DiPix Mar 23 '16 at 11:38
  • None of those should be causing any problems. Try just putting a breakpoint in the `jquery.validate.js` file (on the remote function) –  Mar 23 '16 at 11:46
  • Also never reched the break point [script jquery.validate.js](http://i.imgur.com/XkSEC0D.png) – DiPix Mar 23 '16 at 11:53
  • That means the rules are not being added by `jquery.validate.unobtrusive.js`. Why do you shown the `adapters.add()` method? Have you modified something in the `jquery.validate.unobtrusive.js` file as well? –  Mar 23 '16 at 11:55
  • Yes because here: [Problem - Remote Validation for LIST of MODELs](http://stackoverflow.com/questions/27513472/remote-validation-for-list-of-models) it looks like he changed also `jquery.validate.unobtrusive.js`, anyway in both case doesn't work, never reched break point [jquery.validate.unobtrusive.js code](http://i.imgur.com/lKK0vjg.png) – DiPix Mar 23 '16 at 12:02
  • Well my answer to that question did not involve changing `jquery.validate.unobtrusive.js` (and any change to that is not applicable in you case anyway). There is obviously a problem with your scripts. Are you sure there is nothing else other than what you have shown? And perhaps to test, add a `[Required]` attribute to `Amount` and `@Html.ValidationMessageFor(m => m.FirstSetList[i].Amount)` and edit it to be blank and check the result –  Mar 23 '16 at 12:09
  • Weird, doesn't work. – DiPix Mar 23 '16 at 12:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107131/discussion-between-dipix-and-stephen-muecke). – DiPix Mar 23 '16 at 12:36
  • Anyone have idea why unobtrusive doesn't work? – DiPix Mar 24 '16 at 09:59

0 Answers0