Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi There, can someone please help me, i want to return error alert message doing validation in controller whose return type is PartialViewResult.
Email = Applicant_Service.GetMainContactEmail(Applicant_id, Application_id);
if (Email !="")
{
---Email has value return me view otherwise return me error message.full code is below
return PartialView("../Application/ContactEmailbox");
}
else
{
return error message
}


View Code

$.ajax({
type: "GET",
url: "/CreateApplicationFromSearch/AddUserPartialView_Applicant",
contentType: "application/json; charset=utf-8",
data: { "Applicant_id": ApplicantId, "Application_id": ApplicationId, "CameFrom": "Applicant", contact_person_id: contact_person_id, OfficeHolder_id: OfficeHolder_id, AccountDetail_Id: AccountDetail_Id },
dataType: "html",
success: function (data) {
$('#dialog').html(data);
$('#dialog').dialog('open');
$(".ui-dialog-titlebar-close").hide();
},
error: function () {
alert("Errors arose.");
}
});
Controller code
[HttpGet]
[AllowAnonymous]
public PartialViewResult AddUserPartialView(int Applicant_id, int Application_id, string CameFrom)
{
Email SendEmail = new Email();
string Email = "";
Email = Applicant_Service.GetMainContactEmail(Applicant_id, Application_id);
@ViewBag.ContactEmail = Email;
@ViewBag.Applicant_id = Applicant_id;
@ViewBag.Application_id = Application_id;
@ViewBag.CameFrom = CameFrom;
return PartialView("../Application/ContactEmailbox");
}

What I have tried:

if (Email !="")
{
---Email has value return me view otherwise return me error message.full code is below
return PartialView("../Application/ContactEmailbox");
}
else
{
return error message
}
Posted
Updated 22-May-17 0:25am

1 solution

A quick solution can be to return the JsonResult back to the client side with a flag to and put condition to either alert error message or continue normally, which would be like:

C#
public ActionResult SaveDetailedInfo(Option[] Options)
{
    ActionResult actionResult = null;
    if (Email !="")
    {
       actionResult = Json(new 
                           {
                              status = "Success", 
                              message = PartialView("../Application/ContactEmailbox");
                          });

    }
    else
    {
        actionResult = Json(new 
                              { 
                                status = "Error", 
                                message = "Your Error Message here to show as alert" 
                           });
    }

    return actionResult;
}


Now in the ajax call success, you can check if it was success or error:
JavaScript
success: function (data) {
   if (data.status == "Success") {
                $('#dialog').html(data.message);
                $('#dialog').dialog('open');
                $(".ui-dialog-titlebar-close").hide();
            } else {
                alert(data.message);
            }
}


Hope it helps!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900