Reputation: 924
I have this JS
code below to save the data input by a user. All data is seen in var JsonData = JSON.stringify(data);
. I input a special character in Task (& sign)
[Task Value is (A & B)]
. But in my controller It cut the data before the &
sign.
$('#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"
});
});
Here is my Controller code I put a breakpoint and debug it why after the it reach in this code ovt = jss.Deserialize<OvertimeRequest>((string)overtimeRequest);
it direct to the catch
. And as I mention above it it cuts the data before the the &
sign.
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.";
}
}
Upvotes: 2
Views: 1240
Reputation: 924
I figure it out thanks to all who view my post.
I use this code var task =escape($("#task_ot").val());
Upvotes: 1
Reputation: 218852
You are loosing the data because you are doing a GET request. When you do GET call, The data will be sent as querystring and &
has a special meaning when it is in querystring. It is a seperator for different querystring items.
What you should be doing is, making a POST call to the server. Also there is no need to explicitly doing the deserialization as model binder does that for you. Just create a C# POCO class which represent the structure of data you are sending and use that as your action method parameter.
public class OverTimeViewModel
{
public DateTime DateFiled { set;get;}
public DateTime DateofOvertime { set;get;}
public int EstimatedHours { set;get;}
//Add other properties here
}
and in the javascript code, use POST as the ajax method.
var viewModel = {
DateFiled: '12/12/2012'
EstimatedHours : 10
} ;
// values hard coded for demo. Replace with real values
$.ajax({
type: "POST",
url: urlPA,
data : viewModel
success: function (response) {
//do something with the response
}
});
Now make sure you use the view model we created as the parameter
public ActionResult SaveOvertimeRequest(OverTimeViewModel model)
{
//check model.DateField etc and use
}
Upvotes: 2