Issue
I need to add a warning message to the user, so if they try to insert records that have meeting id == 0 a message is returned making them aware.
My current AngularJS code to save the data, invoking the ImportData method in C#
// Save data to sql database
$scope.SaveData = function (excelData) {
$http({
method: "POST",
url: "/Home/ImportData",
data: JSON.stringify(excelData),
headers: {
'Content-Type': 'application/json'
}
}).then(function (data) {
if (data.status) {
$scope.Message = excelData.length + " Record Successfully Inserted";
$scope.GetData();
}
else {
$scope.Message = "Failed";
}
}, function (error) {
$scope.Message = "Error";
});
};
C# code:
[HttpPost]
public ActionResult ImportData (List<Meeting_WIP> meeting) {
bool status = false;
try {
if (ModelState.IsValid) {
using (MeetingEntities db = new MeetingEntities ()) {
foreach (var i in meeting) {
if (i.MeetingID == 0) {
throw new ArgumentException ("Meeting ID cannot be null or empty string", i.MeetingID.ToString ());
} else {
var compositeKey = db.Meeting_WIP.Find (i.MeetingID, i.AgendaItem);
if (compositeKey == null) {
// Meeting ID does not exist in database it is a new one
db.Meeting_WIP.Add (i);
} else {
// Meeting ID already exist in database just update record
db.Entry (compositeKey).CurrentValues.SetValues (i.MeetingID);
db.Entry (compositeKey).State = EntityState.Modified;
}
}
}
db.SaveChanges ();
status = true;
}
}
} catch (Exception ex) {
ExceptionLogging.SendErrorToText (ex);
}
return new JsonResult { Data = new { status = status } };
}
Currently even if the excel data file contains a Meeting ID == 0, the message that is displayed is:
$scope.Message = excelData.length + " Record Successfully Inserted";
How can I tell tthe Angular code when Meeting ID == 0, then return an error and display something like:
"Meeting ID cannot be zero/null"
screen shot of angularjs code debug
Solution
Since you are throwing argument exception when meeting ID is zero, you should catch that exception and set status and validation message accordingly.
[HttpPost]
public ActionResult ImportData (List<Meeting_WIP> meeting) {
bool status = false;
string message = "";
try {
if (ModelState.IsValid)
{
using (MeetingEntities db = new MeetingEntities ())
{
foreach (var i in meeting)
{
if (i.MeetingID == 0)
{
throw new ArgumentException ("Meeting ID cannot be null or empty string", i.MeetingID.ToString ());
} else
{
var compositeKey = db.Meeting_WIP.Find (i.MeetingID, i.AgendaItem);
if (compositeKey == null)
{
// Meeting ID does not exist in database it is a new one
db.Meeting_WIP.Add (i);
} else
{
// Meeting ID already exist in database just update record
db.Entry (compositeKey).CurrentValues.SetValues (i.MeetingID);
db.Entry (compositeKey).State = EntityState.Modified;
}
}
}
db.SaveChanges ();
status = true;
}
}
} catch (Exception ex) {
ExceptionLogging.SendErrorToText (ex);
} catch (System.ArgumentException ae) {
status = false;
message = "Meeting ID cannot be zero/null";
}
return new JsonResult { Data = new { status = status, message = message } };
}
you can add a new property to your json result for validation message and set value as "Meeting ID cannot be zero/null"
if (data.data.status == true) {
$scope.Message = excelData.length + " Record Successfully Inserted";
$scope.GetData();
}
else {
$scope.Message = data.data.message;
}
Answered By - NTP
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.