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:
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>