1

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.

My ViewModel:

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

My View:

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
        @using (Html.BeginForm())
        {
            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.ValidationMessageFor(model => model.FirstSetList[i].Amount)
                    </td>
                </tr>
            }
            <input type="submit" value="Confirm" class="btn btn-success" />
        }
    </table>
</div>

My Controller:

[HttpPost]
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);
}

UPDATE

Is not the same question as here Remote Validation for LIST of MODELs My Remote Attribute, doesn't send any information to my JsonResult Method, not null, not 0 just nothing! Never reach breakpoint in this method. Also in NETWORK CONSOLE in chrome there aren't any traffic. It looks like AJAX doesn't work in this case and i don't know why?

Community
  • 1
  • 1
DiPix
  • 5,755
  • 15
  • 61
  • 108

1 Answers1

0

Try passing your hole model to your JsonResult Method, like:

Controller

[HttpPost]
public JsonResult Divisibility(ModelName model)
{
    //user your variable name
    model.Value = User.Identity.GetUserId().Where(x => x.Equals("qqqqq"));
    return Json(value == null);
}

Or remove the "HttpPost" just to see if now it's passing values:

Model:

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

Controller

[HttpGet]
public JsonResult Divisibility(int Amount)
{
    var value = User.Identity.GetUserId().Where(x => x.Equals("qqqqq"));

    return Json(value == null);
}
LP. Gonçalves
  • 454
  • 8
  • 26