예제 #1
0
        public ActionResult formSubmit(Category model, string actionType)
        {
            List <string> errorMessage   = new List <string>();
            string        successMessage = Resources.NotifResource.RequestSuccess;
            SessionForSP  sessionParam   = new SessionForSP();

            sessionParam.OrganizationID = new Guid(Session["OrganizationID"].ToString());
            sessionParam.BusinessUnitID = new Guid(Session["BusinessUnitID"].ToString());
            sessionParam.CurrentUserID  = new Guid(Session["CurrentUserID"].ToString());

            if (ModelState.IsValid)
            {
                KeyValuePair <int, string> results = new KeyValuePair <int, string>(1, "");
                model.ModifiedBy = new Guid(Session["CurrentUserID"].ToString());
                if (actionType == "Create")
                {
                    model.CategoryID = Guid.NewGuid();
                    results          = db.Category_Insert(model, sessionParam);
                }
                else if (actionType == "Edit")
                {
                    results = db.Category_Update(model);
                }
                else if (actionType == "ChangeStatus")
                {
                    results = db.Category_ChangeStatus(model);
                }
                else if (actionType == "Delete")
                {
                    results = db.Category_Delete(model);
                }

                if (results.Key == 0 || results.Key == 16 || results.Key == 6)
                {
                    UrlHelper u      = new UrlHelper(this.ControllerContext.RequestContext);
                    string    url    = u.Action("Edit", "Category", new { id = model.CategoryID, success = successMessage });
                    string    urlNew = u.Action("Create", "Category");

                    var jsonData = new { flag = true, Message = url, urlNew = urlNew };
                    return(Json(jsonData));
                }
                else
                {
                    var jsonData = new { flag = false, Message = results.Value.ToString() };
                    return(Json(jsonData));
                }
            }
            else
            {
                foreach (var key in ModelState.Keys)
                {
                    var error = ModelState[key].Errors.FirstOrDefault();
                    if (error != null)
                    {
                        errorMessage.Add(error.ErrorMessage);
                    }
                }
                var jsonData = new { flag = false, Message = errorMessage.First() };
                return(Json(jsonData));
            }
        }