I’m trying to rewrite some data in DB using Ajax, after passing it to controller I always have the same error: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length.
my Ajax code:
tab.on("click", ".edit", function (e) {
var tr = $(this).closest("tr");
var id = tr.data("id");
var fse = $(this).closest("tr").find(".FSE").text();
var field = $(this).closest("tr").find(".ui-widget");
var mon = field.find(".desc_NumM");
var monC = mon.css("background-color");
var tue = field.find(".desc_NumT");
var tueC = tue.css("background-color");
var wed = field.find(".desc_NumW");
var wedC = wed.css("background-color");
var thur = field.find(".desc_NumTr");
var thurC = thur.css("background-color");
var fri = field.find(".desc_NumF");
var friC = fri.css("background-color");
var sat = field.find(".desc_NumSa");
var satC = sat.css("background-color");
var sun = field.find(".desc_NumSu");
var sunC = sun.css("background-color");
var monVal = mon.text();
var tueVal = tue.text();
var wedVal = wed.text();
var thurVal = thur.text();
var friVal = fri.text();
var satrVal = sat.text();
var sunVal = sun.text();
$.ajax({
type: "POST",
url: "/Home/UpdateWeek",
data: {
id: id,
FSE:fse,
Monday: monVal,
Tuesday: tueVal,
Wednesday: wedVal,
Thursday: thurVal,
Friday: friVal,
Saturday: satrVal,
Sunday: sunVal,
MonColor: monC,
TueColor: tueC,
WedColor: wedC,
ThurColor: thurC,
FriColor: friC,
SatColor: satC,
SunColor: sunC
},
dataType: "JSON",
success: function (data) {
alert("Week for: " + fse + " changed!");
$('#weekEvents').load(loadWeekData());
},
error: function () {
alert("Failed, try again");
}
});
});
And here is my controller:
[HttpPost]
public JsonResult UpdateWeek(int id, EventsWeek w)
{
using (WeekEventsDBEntities db = new WeekEventsDBEntities())
{
EventsWeek ewupt = db.EventsWeeks.Where(x => x.Id == id).FirstOrDefault();
ewupt.Monday = w.Monday.Substring(0, 100);
ewupt.Tuesday = w.Tuesday.Substring(0, 100);
ewupt.Wednesday = w.Wednesday.Substring(0, 100);
ewupt.Thursday = w.Thursday.Substring(0, 100);
ewupt.Friday = w.Friday.Substring(0, 100);
ewupt.Saturday = w.Saturday.Substring(0, 100);
ewupt.Sunday = w.Sunday.Substring(0, 100);
ewupt.MonColor = w.MonColor.Substring(0, 50);
ewupt.TueColor = w.TueColor.Substring(0, 10);
ewupt.WedColor = w.WedColor.Substring(0, 10);
ewupt.ThurColor = w.ThurColor.Substring(0, 10);
ewupt.FriColor = w.FriColor.Substring(0, 10);
ewupt.SatColor = w.SatColor.Substring(0, 10);
ewupt.SunColor = w.SunColor.Substring(0, 10);
ewupt.FSE = w.FSE.Substring(0, 20);
db.SaveChanges();
}
return Json(true, JsonRequestBehavior.AllowGet);
}
debbuger always tell that there is a problem with “Substring(0, 50);”. But the funny thing is, if I change “ewupt.MonColor = w.MonColor.Substring(0, 50);” to “ewupt.MonColor = w.MonColor.Substring(0, 10);” it will work ,but not like it should be. Anyway it will be no more error ans data will be passed to my DB… What I’m doing wrong here?
2
Answers
Because you are trying to access the string which is beyond the length of the string.
try checking before using subString()