简体   繁体   中英

C# and ajax call

//Javascript function

function SaveAndUpdateProjectMaster() {

            var CompanyCode = '';
            var ProjectName = '';
            var ProjectDescription = '';
            var ProjectType = '';
            var LocationSite = '';
            var Client = '';
            var Latitude = '';
            var Longitude = '';
            var Status = '';
            var FirstLevelApprover = '';
            var SecondLevelApprover = '';
            var ThirdLevelApprover = '';
            var userComment = '';
            var CrudType = '';
            var Id = '';


            //$('#ddlDivision').val();
            //$('#ddlRegionPrm').val();
            //$('#ddlCountryPrm').val();
            Id = $('#HdxtProjectId').val();
            CompanyCode = $('#ddlCompanyPrm').val();
            ProjectName = $('#txtProjectnamePrm').val();
            ProjectDescription = $('#txtProjectDescriptionPrm').val();
            ProjectType = $('#ddlProjecttypePrm').val();
            LocationSite = $('#TxtLocationPrm').val();
            Client = $('#TxtClientPrm').val();
            Latitude = $('#TxtLatitudePrm').val();
            Longitude = $('#TxtLongitudePrm').val();
            Status = $('#hdnPMStatus').val();
            FirstLevelApprover = $('#TxtFirstLevelPrm').val();
            SecondLevelApprover = $('#TxtSecondLevelPrm').val();
            ThirdLevelApprover = $('#TxtThirdlevel').val();
            userComment = $('#txtUserComment').val();
            CrudType=$('#hdnBtnType').val();

            var obj = {};

            obj.Id = Id;
            obj.CompanyCode = CompanyCode;
            obj.ProjectName = ProjectName;
            obj.ProjectDescription = ProjectDescription;
            obj.ProjectType = ProjectType;
            obj.LocationSite = LocationSite;
            obj.Client = Client;
            obj.Latitude = Latitude;
            obj.Longitude = Longitude;
            obj.Status = Status;
            obj.FirstLevelApprover = FirstLevelApprover;
            obj.SecondLevelApprover = SecondLevelApprover;
            obj.ThirdLevelApprover = ThirdLevelApprover;
            obj.userComment = userComment;
            obj.CrudType = CrudType;


            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "ProjectMaster.aspx/SaveAndUpdateProject",
                //data: JSON.stringify({ Id:Id,CompanyCode: CompanyCode, ProjectName: ProjectName, ProjectDescription: ProjectDescription, ProjectType: ProjectType, LocationSite: LocationSite, Client: Client, Latitude: Latitude, Longitude: Longitude, Status: Status, FirstLevelApprover: FirstLevelApprover, SecondLevelApprover: SecondLevelApprover, ThirdLevelApprover: ThirdLevelApprover, userComment: userComment, CrudType: CrudType }),
                data: JSON.stringify({ Id: Id, CompanyCode: CompanyCode, ProjectName: ProjectName, ProjectDescription: ProjectDescription, ProjectType: ProjectType, LocationSite: LocationSite, Client: Client, Latitude: Latitude, Longitude: Longitude, Status: Status, FirstLevelApprover: FirstLevelApprover, SecondLevelApprover: SecondLevelApprover, ThirdLevelApprover: ThirdLevelApprover, userComment: userComment, CrudType: CrudType }),
                dataType: "json",
                success: function (data) {

                    if (data != '') {
                        debugger;
                        data = $.parseJSON(data.d);


                        data = $.parseJSON(data.d);
                        $('#ProjectMasterModal').hide();

                        var divison = $('#ddlDivision').val();
                        var country = $('#ddlCountryPrm').val();
                        var company = $('#ddlCompanyPrm').val();
                        var project = $('#ddlProjMaster').val();
                        //var masterVal = $('#hdnPMStatus').val();
                        GetRecord(divison,country,company,project,1);

                        alert(data.Msg);
                        CancelWarningCategory();

                    }

                }
            });

        }

//C# WebMethod

        [WebMethod]
        public static string SaveAndUpdateProject(string Id,string CompanyCode, string ProjectName, string ProjectDescription, string ProjectType,string LocationSite, string Client, string Latitude, string Longitude, string Status, string FirstLevelApprover, string SecondLevelApprover,string ThirdLevelApprover,string userComment,string CrudType)
        //public static string SaveAndUpdateProject(string Id)
        {

            //string CompanyCode=string.Empty;
            //string ProjectName=string.Empty;
            //string ProjectDescription=string.Empty;
            //string ProjectType=string.Empty;
            //string LocationSite=string.Empty;
            //string Client=string.Empty;
            //string Latitude=string.Empty;
            //string Longitude=string.Empty;
            //string Status=string.Empty;
            //string FirstLevelApprover=string.Empty;
            //string SecondLevelApprover=string.Empty;
            //string ThirdLevelApprover=string.Empty;
            //string userComment=string.Empty;
            //string CrudType=string.Empty;


            string res = string.Empty;
            UserMsg obj = new UserMsg();
            try
            {

                String Result = string.Empty;
                BLL_ManageProject BLLProject = new BLL_ManageProject();
                //if (btnProjectsave.Text == "Add New")
                //{
                if (CrudType == "Add New")
                {
                    SqlParameter[] paramSaveProject = new SqlParameter[19];
                    paramSaveProject[0] = new SqlParameter("@CompanyCode", CompanyCode);
                    paramSaveProject[1] = new SqlParameter("@ProjectName", Convert.ToString(ProjectName));
                    paramSaveProject[2] = new SqlParameter("@ProjectDescription", Convert.ToString(ProjectDescription));
                    paramSaveProject[3] = new SqlParameter("@ProjectType", ProjectType);
                    paramSaveProject[4] = new SqlParameter("@LocationSite", Convert.ToString(LocationSite));
                    paramSaveProject[5] = new SqlParameter("@Client", Convert.ToString(Client));
                    paramSaveProject[6] = new SqlParameter("@Latitude", Convert.ToString(Latitude == "" ? "0.00" : Latitude));
                    paramSaveProject[7] = new SqlParameter("@Longitude", Convert.ToString(Longitude == "" ? "0.00" : Longitude));
                    paramSaveProject[8] = new SqlParameter("@IsActive", Convert.ToInt32(Status));
                    paramSaveProject[9] = new SqlParameter("@FirstLevelApprover", Convert.ToString(FirstLevelApprover));
                    paramSaveProject[10] = new SqlParameter("@SecondLevelApprover", Convert.ToString(SecondLevelApprover));
                    paramSaveProject[11] = new SqlParameter("@ThirdLevelApprover", Convert.ToString(ThirdLevelApprover));
                    paramSaveProject[12] = new SqlParameter("@PageName", HttpContext.Current.Request.Url.Segments.Last());
                    paramSaveProject[13] = new SqlParameter("@Title", "Insert Project Master");
                    paramSaveProject[14] = new SqlParameter("@UserComment", userComment);
                    paramSaveProject[15] = new SqlParameter("@SystemComment", "");
                    paramSaveProject[16] = new SqlParameter("@Revision", 0);
                    paramSaveProject[17] = new SqlParameter("@CreatedBy", (!string.IsNullOrEmpty(HttpContext.Current.Session["WindowsID"].ToString())) ? HttpContext.Current.Session["WindowsID"].ToString() : null);
                    paramSaveProject[18] = new SqlParameter("@CreatedDate", System.DateTime.Now);
                    Result = BLLProject.SaveProjectMaster(paramSaveProject);
                    if (Result == "1")
                    {

                        obj.Code = "1";
                        obj.Msg = GlobalConst.strInsertSuccess;
                    }
                    else if (Result == "2")
                    {

                        obj.Code = "2";
                        obj.Msg = GlobalConst.strRecordExists;
                    }
                    else
                    {

                        obj.Code = "0";
                        obj.Msg = GlobalConst.strInsertFaliure;
                    }
                }
                else if (CrudType == "Update")
                {
                    SqlParameter[] paramSaveProject = new SqlParameter[20];
                    paramSaveProject[0] = new SqlParameter("@CompanyCode",CompanyCode);
                    paramSaveProject[1] = new SqlParameter("@ProjectName", Convert.ToString(ProjectName));
                    paramSaveProject[2] = new SqlParameter("@ProjectDescription", Convert.ToString(ProjectDescription));
                    paramSaveProject[3] = new SqlParameter("@ProjectType", ProjectType);
                    paramSaveProject[4] = new SqlParameter("@LocationSite", Convert.ToString(LocationSite));
                    paramSaveProject[5] = new SqlParameter("@Client", Convert.ToString(Client));
                    paramSaveProject[6] = new SqlParameter("@Latitude", Convert.ToString(Latitude == "" ? "0.00" : Latitude));
                    paramSaveProject[7] = new SqlParameter("@Longitude", Convert.ToString(Longitude == "" ? "0.00" :Longitude));
                    paramSaveProject[8] = new SqlParameter("@IsActive", Convert.ToInt32(Status));
                    paramSaveProject[9] = new SqlParameter("@FirstLevelApprover", Convert.ToString(FirstLevelApprover));
                    paramSaveProject[10] = new SqlParameter("@SecondLevelApprover", Convert.ToString(SecondLevelApprover));
                    paramSaveProject[11] = new SqlParameter("@ThirdLevelApprover", Convert.ToString(ThirdLevelApprover));
                    paramSaveProject[12] = new SqlParameter("@ProjectId", Id);
                    paramSaveProject[13] = new SqlParameter("@PageName",HttpContext.Current.Request.Url.Segments.Last());
                    paramSaveProject[14] = new SqlParameter("@Title", "Update Project Master");
                    paramSaveProject[15] = new SqlParameter("@UserComment", userComment);
                    paramSaveProject[16] = new SqlParameter("@SystemComment", "");
                    paramSaveProject[17] = new SqlParameter("@Revision", 0);
                    paramSaveProject[18] = new SqlParameter("@CreatedBy", (!string.IsNullOrEmpty(HttpContext.Current.Session["WindowsID"].ToString())) ? HttpContext.Current.Session["WindowsID"].ToString() : null);
                    paramSaveProject[19] = new SqlParameter("@CreatedDate", System.DateTime.Now);
                    Result = BLLProject.UpdateProjectMaster(paramSaveProject);
                    if (Result == "1")
                    {
                        obj.Code = "1";
                        obj.Msg = GlobalConst.strUpdateSuccess;
                        //BindProjectGrid(ddlDivision.SelectedItem.Value, ddlCountryPrm.SelectedItem.Value, ddlCompanyPrm.SelectedItem.Value, ddlProjMaster.SelectedValue.ToString());
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "Popup", "ShowPopup('" + GlobalConst.strUpdateSuccess + "');", true);
                        //btnCancelProject_Click(sender, e);
                    }
                    else if (Result == "2")
                    {
                        obj.Code = "2";
                        obj.Msg = GlobalConst.strUpdateSuccess;
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "Popup", "ShowPopup('" + GlobalConst.strRecordExists + "');", true);
                        //btnCancelProject_Click(sender, e);
                    }
                    else
                    {
                        obj.Code = "0";
                        obj.Msg = GlobalConst.strUpdateFaliure;
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "Popup", "ShowPopup('" + GlobalConst.strUpdateFaliure + "');", true);
                        //btnCancelProject_Click(sender, e);
                    }
                }

            }
            catch (Exception ex)
            {
                var myActualType = typeof(ProjectMaster);
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "Popup", "ShowPopup('" + GlobalConst.strErrorMessge + "');", true);
                SecurLog.ErrorLogInfoSave(DateTime.Now, Convert.ToString(HttpContext.Current.Session["WuserCode"]), ex.Source, myActualType.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, "1", ex.Message, ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(' ')), "1", HttpContext.Current.Request.Url.AbsoluteUri, "SECUR", Convert.ToString(HttpContext.Current.Session["ServiceCo"]));
                SecurLog.SendErrorMail(DateTime.Now, Convert.ToString(HttpContext.Current.Session["WuserCode"]), ex.Source, myActualType.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, "1", ex.Message, ex.StackTrace.Substring(ex.StackTrace.LastIndexOf(' ')), "1", HttpContext.Current.Request.Url.AbsoluteUri, "SECUR", Convert.ToString(HttpContext.Current.Session["ServiceCo"]));
                obj.Code = "0";
                obj.Msg = ex.Message;
            }

            return JsonConvert.SerializeObject(obj);
        }

In the above code a mentioned javascript function and C# webmethod the ajax call is not working, not showing any errors and not hitting the break point. please help me thanks in advance!!!!!!!!!!!!!

But the above is working with 5 parameters but not working and not showing any errors where more than 10 parameters is passed to ajax call

please help me.......... Thanks in advance

Can you please add model for the query string params

public class ProjectModel
    {
        public int Id { get; set; }
        public string CompanyCode { get; set; }
        public string ProjectName { get; set; }
        public string ProjectDescription { get; set; }
        public string ProjectType { get; set; }
        public string LocationSite { get; set; }
        public string Client { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string Status { get; set; }
        public string FirstLevelApprover { get; set; }
        public string SecondLevelApprover { get; set; }
        public string ThirdLevelApprover { get; set; }
        public string userComment { get; set; }
        public string CrudType { get; set; }
    }

and pass model in action method

[WebMethod]
public static string SaveAndUpdateProject(ProjectModel model)
{
// your business logic
// you can access model properties by model.Id, model.CompanyCode
}

Also, in ajax method, you need to change your obj object

var model= new Object();  
model.Id = $('#HdxtProjectId').val();
model.CompanyCode = $('#ddlCompanyPrm').val();
model.ProjectName = $('#txtProjectnamePrm').val();
// rest of the properties

And in Ajax method, update data to :

data: JSON.stringify(model),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM