在我的应用程序中,我有一个编辑个人资料页面,您可以在下面看到;
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master"
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.EmployeeViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm("Edit", "Employee"))
{%>
<%: Html.AntiForgeryToken()%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Edit Employee</legend>
<% if (TempData["Message"] != null && TempData["Message"].ToString().Length > 0)
{ %>
<p class="success">
At <% Response.Write(DateTime.Now.ToString("T")); %>
you entered <%: TempData["Message"]%>.
</p>
<%} %>
<table class="groupBorder" id="editTable">
<tr>
<td style="text-align: right;"><%: Html.LabelFor(model => model.Employee.CompanyId)%></td>
<td>
<%: Html.DropDownListFor(model => model.Employee.CompanyId, Model.CompaniesSelectList)%>
</td>
</tr>
<tr>
<td style="text-align: right;">
<%: Html.LabelFor(model => model.Employee.EmployeeNumber)%>
</td>
<td>
<%: Html.TextBoxFor(model => model.Employee.EmployeeNumber,
new { maxlength = "4", style = "width:35px;text-transform: uppercase;" })%>
<%: Html.ValidationMessageFor(model => model.Employee.EmployeeNumber)%>
<span class="error" id="errorMessage">
<% if (TempData["ErrorMessage"] != null)
{ %>
<%: TempData["ErrorMessage"].ToString()%>
<% } %>
</span>
</td>
</tr>
<tr>
<td style="text-align: center;padding-top:20px;" colspan="2">
<input type="submit" value="Save" id="btnSubmit"/></td>
</tr>
</table>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "List")%>
</div>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#Employee_CompanyId").change(function () {
validateEmployeeNumber();
});
$("#Employee_EmployeeNumber").change(function () {
validateEmployeeNumber();
});
});
function validateEmployeeNumber() {
var employeeNumber = $("#Employee_EmployeeNumber").val().toUpperCase();
$("#Employee_EmployeeNumber").val(employeeNumber);
if (employeeNumber.length == 0) {
$('#errorMessage').text("Employee Number Required");
}
if (employeeNumber.length > 0) {
$('#errorMessage').text('');
$('#btnSubmit').attr("disabled", false);
var companyId = $("#Employee_CompanyId").val();
var employeeId = $("#Employee_EmployeeId").val();
$.ajax({
data: { companyId: companyId, employeeNumber: employeeNumber, employeeId: employeeId },
dataType: "text",
success: function (data) {
if (data.length > 0) {
$('#errorMessage').text(data);
$('#btnSubmit').attr("disabled", true);
}
},
type: "POST",
url: '<%= Url.Action("ValidateEmployeeNumber", "Employee")%>'
});
}
}
</script>
</asp:Content>
这里使用的控制器如下;
[HttpPost]
[Authorize(Roles = "Administrator, ManagerAccounts, ManagerIT")]
public string ValidateEmployeeNumber(string companyId, string employeeNumber, string employeeId = null)
{
var cid = 0;
var flag = int.TryParse(companyId, out cid);
if (!flag)
{
return string.Empty;
}
var eid = 0;
if (employeeId != null)
{
flag = int.TryParse(employeeId, out eid);
if (!flag)
{
return string.Empty;
}
}
if (Employee.ValidateEmployeeNumberFormat(employeeNumber) == false)
{
return "Invalid format";
}
var employeeNumberExists = Employee.GetEmployeeByEmployeeNumber(cid, employeeNumber, eid);
return employeeNumberExists ? "Invalid Employee Number already in use." : string.Empty;
}
此页面本身已被编辑以显示我感兴趣的位。该页面使管理员能够编辑员工的个人资料,包括员工编号。对服务器的 Ajax 调用是为了检查员工编号是否唯一。现在在我的开发人员机器上,这适用于所有服务器;开发、测试和生产。但是对于一些在其他机器上工作的人来说,它不起作用。当他们尝试在生产服务器上编辑员工编号时,突然在 AJAX 调用后立即出现大量红色的 javascript 文本。为什么会发生这种情况,如何解决?