所以,我有这种超级奇怪的场景。这是它如何崩溃。我有不同的授权角色,用户可以在“管理工具”中编辑个人资料。有一个这种能力的地方似乎表现得超级傻了,我不能为我的生活弄清楚为什么!当你担任SuperUsersRole的角色时,代码工作正常。但是,当你担任StandardUsers角色的角色时,我得到这个javascript错误而且失败了。而且,这是另一个踢球者,它只在代码未在本地运行时失败。这是用MVC编写的,所以不用多说,这里是代码:
观点:
@{
ViewBag.Title = "ChangeUsername";
}
@using (Ajax.BeginForm("ChangeUsername", "User", new { uniqueUserId = @Model.UserGuid }, new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "changeUname" }))
{
<div class="form_section">
<h3>Update User Name:
</h3>
<div class="form_item">
<div class="frm_label">
<label for="username">
@AdminResource.CurrentUserName
</label>
</div>
<div class="item">
<p>@Model.UserName</p>
</div>
</div>
<div class="form_item">
<div class="frm_label">
<label for="username">
@AdminResource.UserName
</label>
</div>
<div class="item">
@Html.TextBoxFor(model => model.NewUsername, new { id = "uName_change" }) @Html.ValidationMessageFor(model => model.NewUsername)
@if (Model.AlternateUsernames != null && Model.AlternateUsernames.Count() != 0)
{
<p>
<strong>Suggested usernames:</strong><br />
@foreach (var username in Model.AlternateUsernames)
{
<input type="radio" name="un" value="@username" onclick="setText(this);" />@username<br />
}
</p>
}
</div>
</div>
<div class="form_item">
<div class="frm_label">
<label for="password">
</label>
</div>
<div class="item">
<input type="submit" name="submit" value="Update Username" class="btn_primary" />
</div>
</div>
</div>
@Html.HiddenFor(model => model.UserGuid)
@Html.HiddenFor(model => model.UserName)
}
视图模型:
namespace AdminTool.Models.ViewModel
{
public class ChangeUsername
{
[Required(ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "UserNameErrorRequired")]
[Display(ResourceType = typeof(AdminResource), Name = "UserName", Description = "UserNameDescription")]
[NotRegEx(@"^[Mm][rR][Ii]\d{4}|^[mM]\d{6}", ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "UserNameAlreadyExists")]
[RegularExpression(@"^([a-zA-Z0-9!\@#\$%\^&\(\)-_\+\.'`~/=\?\{\}\|]){6,255}$", ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "UserNameFormatError")]
public string NewUsername { get; set; }
public String UserName { get; set; }
public Guid UserGuid { get; set; }
public List<string> AlternateUsernames { get; set; }
}
}
控制器中的两个相应方法:
[HttpGet]
[ClaimsPrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Resource = Resources.User, Operation = Operations.Edit)]
public ActionResult ChangeUsername(Guid uniqueUserId)
{
User user = UserManager.GetUser(uniqueUserId);
AuthorizationHelper.ConfirmAccess(Resources.User, Operations.Edit, user);
ChangeUsername model = new ChangeUsername(){UserGuid = user.Identifier,NewUsername = user.Username};
return View(model);
}
[HttpPost]
[ClaimsPrincipalPermission(System.Security.Permissions.SecurityAction.Demand, Resource = Resources.User, Operation = Operations.Edit)]
public ActionResult ChangeUsername(ChangeUsername changeUsername)
{
User usr = UserManager.GetUser(changeUsername.UserGuid);
AuthorizationHelper.ConfirmAccess(Resources.User, Operations.Edit, usr);
if (!ModelState.IsValid)
{
return View(changeUsername);
}
//setup request
SupportUser su = (SupportUser)ViewData["SupportUser"];
RequestData rqd = new RequestData(GetApplicationIdentifer(usr), su.clientIP, su.lanID);
UserUsernameChangeRequest request = new AdminUsernameChangeRequest(rqd,usr,changeUsername.NewUsername);
UserUsernameChangeResponse response = UserManager.ChangeUserUsername(request);
if (response.Status == UserProcessorStatus.Success)
{
var message = ("User has been updated successfully");
TempData["message"] = message;
return PartialView("_NewUsername", changeUsername);
}
switch (response.Status)
{
case UserProcessorStatus.DuplicateUsername:
{
ModelState.AddModelError("NewUsername", "Duplicate username");
changeUsername.AlternateUsernames = response.AlternateUsernames;
return PartialView(changeUsername);
}
default:
{
ModelState.AddModelError("NewUsername", String.Format("An unexpected error occured. Error Code:{0}, Message:{1}", response.Status, response.Message));
return PartialView(changeUsername);
}
}
}
就像我之前说过的那样,这只发生在你担任某个角色时,当代码没有在本地运行时,这对我来说是令人费解的。提前谢谢!
编辑** 这是控制台说错误的来源,这是一个jquery.unobtrusive.min.js文件:
/ * **用于jQuery的不显眼的Ajax支持库 **版权所有(C)Microsoft Corporation。版权所有。 * / (function(a){var b =“unobtrusiveAjaxClick”,g =“unobtrusiveValidation”; function c(d,b){var a = window,c =(d ||“”)。split(“。”); while (a&amp;&amp; c.length)a = a [c.shift()]; if(typeof a ===“function”)返回a; b.push(d);返回Function.constructor.apply(null, b)}函数d(a){return a ===“GET”|| a ===“POST”}函数f(b,a){!d(a)&amp;&amp; b.setRequestHeader(“X -HTTP-Method-Override“,a)}函数h(c,b,e){var d; if(e.indexOf(”application / x-javascript“)!== - 1)return; d =(c .getAttribute(“data-ajax-mode”)||“”)。toUpperCase(); a(c.getAttribute(“data-ajax-update”))。each(function(f,c){var e; switch (d){情况下 “之前”:E = c.firstChild;一个( “”)的HTML(b)中.contents()每个(函数(){c.insertBefore(此,E)});打破;案件。 “之后”:一( “”)的HTML(b)中.contents()每个(函数(){c.appendChild(本)});断裂;默认:A(C)的.html(b)}})函数e(b,e){var j,k,g,i; j = b.getAttribute(“data-ajax-confirm”); if(j&amp;&amp;!window.confirm(j))return; k = A(b.getAttribute( “数据AJAX加载”)); I = b.getAttribute( “数据AJAX加载持续时间”)|| 0; a.extend(例如,{类型:b.getAttri bute(“data-ajax-method”)|| undefined,url:b.getAttribute(“data-ajax-url”)|| undefined,beforeSend:function(d){var a; f(d,g); a = c(b.getAttribute(“data-ajax-begin”),[“xhr”])。apply(this,arguments); a!== false&amp;&amp; k.show(i); return a},complete :函数(){。k.hide(I); C(b.getAttribute( “数据AJAX完成”),[ “XHR”, “状态”])申请(在此,自变量)},成功:函数( A,E,d){H(b,A,d.getResponseHeader( “内容类型”)|| “text / html的”); C(b.getAttribute( “数据Ajax的成功”),[“数据”, “状态”, “XHR”])申请(在此,自变量)},错误:C(b.getAttribute( “数据Ajax的失败”),[ “XHR”, “状态”, “错误”] )}); e.data.push({名称: “X-请求-带有” 值:! “的XMLHttpRequest”}); G = e.type.toUpperCase();如果(d(克)){即type =“POST”; e.data.push({name:“X-HTTP-Method-Override”,value:g})} a.ajax(e)} function i(c){var b = a(c )。数据(克);返回b || || b.validate b.validate()}一( “A [数据AJAX =真]”)生活( “点击”,功能(!)。{一个.preventDefault(); e(this,{url:this.href,type:“GET”,data:[]})}); a(“form [data-ajax = true] input [type = image]”) .live( “点击”,功能(C){VAR G = c.target.name,d = A(c.target)中,f = d.parents( “形式”)[0],E = d.offset();一(F)。数据(B,[{名: “X” 克+,值:Math.round(c.pageX-e.left)},{名称: “y” 的G +,值:Math.round(c.pageY-e.top)}]) ; setTimeout(function(){a(f).removeData(b)},0)}); a(“form [data-ajax = true]:submit”)。live(“click”,function(c){ var e = c.target.name,d = a(c.target).parents(“form”)[0]; a(d).data(b,e?[{name:e,value:c.target .value的}]:[]); setTimeout的(函数(){A(d).removeData(b)},0)});一个( “形式[数据AJAX =真]”)生活( “提交” ,function(d){var c = a(this).data(b)|| []; d.preventDefault(); if(!i(this))return; e(this,{url:this.action,类型:this.method || “GET”,数据:c.concat(一个(本).serializeArray())})})})(jQuery的);