jeudi 10 mars 2016

Why must my return statement be outside a conditional with asp.net mvc c# controller method

I have a Try/Catch with an If/Else

In the If/Else both do a return

Thus I don't understand why I would need to place my return OUTSIDE of the if statement.

Plus if I did that the problem if that I end up needing to write additional code to handle which model I want to be used. Is there a good design pattern that can solve my problem?

 public JsonResult GetReportGroupAssignments(int? id)
    {

        var model = new List<ReportGroup>();
        var defModel = new List<ReportDefinition>();

        try
        {

            if (id == null)
            {
                model = _reportService.GetReportGroups("en-us").ToList();
                return Json(model, JsonRequestBehavior.AllowGet);
            }
            else
            {

                defModel = _reportService.GetReportDefinitions().ToList();
                return Json(defModel, JsonRequestBehavior.AllowGet);
            }




        }
        catch (Exception ex)
        {

            Logger.Error(ex, "Error loading LoadReportList.");
        }




    }

Aucun commentaire:

Enregistrer un commentaire