我有这JS
代码以保存用户输入的数据。所有数据在var JsonData = JSON.stringify(data);
。我输入了一个特殊字符Task (& sign)
[Task Value is (A & B)]
。但在我的控制器中,它在&
标志前削减了数据。如何保存c#asp.net中的特殊字符
$('#btn_SubmitOT').live('click', function() {
if (lastSel != -1) {
$('#' + lastSel + '_isValidated').val(true);
// $('#' + lastSel + '_RequestedBy').val();
var datefiled = $('#ot_dateFiled').val().
$('#' + lastSel + '_OTDateApplied').val(datefiled);
jQuery('#grdOTApplication').saveRow(lastSel, false, 'clientArray');
}
var task = $("#task_ot").val();
//var data = $("#grdOTApplication").jqGrid('getRowData');
var data = {
DateFiled: $("#ot_dateFiled").val(),
DateofOvertime: $("#ot_dateOfOT").val(),
EmployeeId: $("#empApplicants").val(),
In1: $("#from_sup").val() + $("#from_AM_PM").val(),
Out1: $("#to_sup").val() + $("#to_AM_PM").val(),
EstimatedHours: $("#estimateHrsWrk_ot").val(),
SpecificTask: task,
ApprovedBy: $("#approveofficial").val() != null ? $("#approveofficial").val() : 0,
RecommendedBy: $("#recommenders").val() != null ? $("#recommenders").val() : 0,
SupervisedBy: $("#immediatesupervisor").val() != null ? $("#immediatesupervisor").val() : 0,
IsCOC: $('#cmbCO').val() == 1 ? true : false
}
var JsonData = JSON.stringify(data);
var urlPA = '../Request/saveOvertimeRequest?overtimeRequest=' + JsonData + '&_role = 3;
$.ajax({
type: "GET",
url: urlPA,
success: function (response) {
alert(response);
$("#grdOTApplication").trigger("reloadGrid", [{
current: true
}]);
$("#grdOTHistory").trigger("reloadGrid", [{
current: true
}]);
$("#ot_dateOfOT").val("");
$("#from_sup").val("__:__");
$("#to_sup").val("__:__");
$("#estimateHrsWrk_ot").val("");
$("#task_ot").val("");
},
error: function (response) {
alert("Error");
},
datatype: "text"
});
});
这里是我的控制器代码,我把一个断点,并调试它为什么在这个代码ovt = jss.Deserialize<OvertimeRequest>((string)overtimeRequest);
在它到达后直接到catch
。正如我上面提到的那样,它会在&
之前削减数据。
public String saveOvertimeRequest(String overtimeRequest, int _role)
{
Nullable<DateTime> MyNullableDate = null;
try
{
Int32 requestedBy = Convert.ToInt32(HttpContext.Current.Session["PersonId"]);
Int32 empId = Convert.ToInt32(HttpContext.Current.Session["EmpId"]);
OvertimeRequest ovt = new OvertimeRequest();
JavaScriptSerializer jss = new JavaScriptSerializer();
ovt = jss.Deserialize<OvertimeRequest>((string)overtimeRequest);
ovt.IsFromESS = true;
ovt.RequestedBy = requestedBy;
if (_role == 3 || ((ovt.SupervisedBy == 0 || ovt.SupervisedBy == null) && ovt.RecommendedBy > 0 && ovt.ApprovedBy > 0))
{
ovt.SupervisedBy = null;
ovt.DateSupervised = DateTime.Now;
}
if (_role == 4 || ((ovt.SupervisedBy == 0 || ovt.SupervisedBy == null) && (ovt.RecommendedBy == 0 || ovt.RecommendedBy == null) && ovt.ApprovedBy > 0))
{
ovt.SupervisedBy = null;
ovt.DateSupervised = DateTime.Now;
ovt.RecommendedBy = null;
ovt.DateRecommend = DateTime.Now;
}
if (_role == 5 || ((ovt.SupervisedBy == 0 || ovt.SupervisedBy == null) && (ovt.RecommendedBy == 0 || ovt.RecommendedBy == null) && (ovt.ApprovedBy == 0 || ovt.ApprovedBy == null)))
{
ovt.SupervisedBy = null;
ovt.DateSupervised = DateTime.Now;
ovt.RecommendedBy = null;
ovt.DateRecommend = DateTime.Now;
ovt.ApprovedBy = null;
ovt.DateApproved = DateTime.Now;
ovt.IsPosted = true;
ovt.DatePosted = DateTime.Now;
}
try
{
db.AddToOvertimeRequests(ovt);
db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
catch (Exception e)
{
return "An error has occured while sending your request.";
}
}
如果我用这个方法可以所有的特殊字符可以节省? – KiRa