C#MVC5 JsonResult

时间:2015-11-29 17:44:01

标签: javascript c# jquery asp.net-mvc-5 jsonresult

我正在尝试检查我的应用程序中是否采用了CustomUrl。问题是,当我单击提交按钮时,没有任何反应。我很难理解我做错了什么。

澄清:没有javascript错误,页面没有提交。对我来说,任何地方都没有任何错误。此外,如果我从AccountViewModel中删除 [Remote] 部分,因此它不会尝试进行检查,页面将提交,它也会在数据库中记录该值。所以我相当肯定它与我试图实施的验证有关。

以下是代码:

MembersController.cs

public JsonResult IsCustomUrlInUse(string customUrl)
{
    return Json(!UserManager.Users.Any(x => x.CustomUrl == customUrl), JsonRequestBehavior.AllowGet);
}

AccountViewModel.cs

[Required]
[StringLength(20, MinimumLength = 3)]
[Display(Name = "Custom URL")]
[Remote("IsCustomUrlInUse", "Members", ErrorMessage="Custom Url is already in use.  Please choose another.")]
public string CustomUrl { get; set; }

Register.cshtml

@model Azularis.System.Events.Models.RegisterViewModel
@{
    ViewBag.Title = "Register";
}

<h2>@ViewBag.Title.</h2>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script> 

@using (Html.BeginForm("Register", "Members", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.CustomUrl, new { @class = "col-md-2 control-label"})
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.CustomUrl, new { @class = "form-control"})
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

编辑:

在评论中回答问题:

  1. “开发者”选项中的“网络”选项卡正在捕获“自定义URL”字段的条目。
  2. 当我删除Remote时,最小字符的验证会启动并显示错误。此外,即使Remote在那里,最小字符限制仍然有效。
  3. 网络标签示例:http://postimg.org/image/8e2e1hesx/

    我还在视图中删除了捆绑包,以确保由于重复而没有发生这种情况,但仍然会发生同样的事情。

    编辑2: 我在IsCustomUrlInUse方法中添加了一个Logging行,但它永远不会被触发。是不是我需要启用json调用服务器?因为MVC阻止json调用,直到我在某个地方的设置中启用它?

    编辑3:

    我设法产生了这个错误,我不知道我怎么也无法复制它,但也许这会有所帮助:

    2015-11-29 13:50:19.4659||System.Web.HttpException (0x80004005): The controller for path '/__browserLink/requestData/c244808430ad49a5afee6a0ecb685cf7' was not found or does not implement IController.
       at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
       at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    2015-11-29 13:50:47.0584||System.Web.HttpException (0x80004005): The controller for path '/__browserLink/requestData/c244808430ad49a5afee6a0ecb685cf7' was not found or does not implement IController.
       at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
       at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    

0 个答案:

没有答案