【问题标题】:Remote Attribute doesn't work in MVC ASP.NET [duplicate]远程属性在 MVC ASP.NET 中不起作用 [重复]
【发布时间】:2016-03-22 16:07:22
【问题描述】:

我需要制作自己的验证函数,我发现了这个:http://www.tugberkugurlu.com/archive/asp-net-mvc-remote-validation-for-multiple-fields-with-additionalfields-property

我试图使用远程属性,但是 chrome 没有向我的 JsonResult 方法发送任何信息,我不知道为什么。

我的视图模型:

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

我的观点:

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

我的控制器:

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

更新

和这里Remote Validation for LIST of MODELs不是同一个问题 我的远程属性,不向我的 JsonResult 方法发送任何信息,不为空,不为 0,什么都没有!永远不要在此方法中达到断点。同样在 chrome 的 NETWORK CONSOLE 中也没有任何流量。看起来 AJAX 在这种情况下不起作用,我不知道为什么?

【问题讨论】:

  • 试试int Amount
  • 你能在控制台上检查是否使用chrome,如果是firefox firebug控制台,看看它是否在传递值?
  • 什么都没有,就是空的。
  • 在文本框中输入时看不到 ajax 请求?
  • 没错,网络控制台 (Chrome) 中没有任何内容

标签: c# json asp.net-mvc validation


【解决方案1】:

尝试将您的孔模型传递给您的 JsonResult 方法,例如:

控制器

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

或者删除“HttpPost”只是为了看看它现在是否在传递值:

型号:

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

控制器

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

    return Json(value == null);
}

【讨论】:

  • 什么都没有,在这两种情况下,chrome 中的网络控制台都是空的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2019-07-17
  • 2016-06-17
  • 2014-03-26
  • 1970-01-01
相关资源
最近更新 更多