将context.Request.Params转换为整数和字符串

时间:2018-09-11 08:29:29

标签: jquery asp.net ajax webforms generic-handler

我正在使用具有通用句柄的jquery ajax将记录提交到sql server 2012数据库,但是我的代码未将context.Request.Params [“ city”]转换为整数 这是我的通用处理程序代码:

if (context.Request.Files.Count > 0)
    {

        JavaScriptSerializer js = new JavaScriptSerializer();

        var companyname = context.Request.Params["Companyname"];
        var city = context.Request.Params["city"];
        var email = context.Request.Params["email"];
        var phone = context.Request.Params["phone"];
        var address = context.Request.Params["address"];
        var postalcode = context.Request.Params["postalcode"];
        var contactpersonname = context.Request.Params["contactpersonname"];
        var contactpersonemail = context.Request.Params["contactpersonalemail"];
        var contactpersonphone = context.Request.Params["contactpersonphone"];
        var password = context.Request.Params["passwords"];
        var website = context.Request.Params["website"];
        var companysize = context.Request.Params["companysize"];
        var companyrevenue = context.Request.Params["companyrevenue"];
        var patner = context.Request.Params["patner"];

        P.PatnerFormsName = companyname;
        P.PatnerFormsCity = Convert.ToInt32(city);

        P.PatnerFormsEmail = email;
        P.PatnerFormsPhone = phone;
        P.PatnerFormsAddress = address;
        P.PatnerFormsPostalcode = postalcode;
        P.PatnerFormsContactPersonname = contactpersonname;
        P.PatnerFormsContactPersonEmail = contactpersonemail;
        P.PatnerFormsContactPersonPhone = contactpersonphone;
        P.PatnerFormsWebsite = website;
        P.PatnerFormsCompanysize = companysize;
        P.PatnerFormsAunnualRevenue = companyrevenue;
        P.PatnerFormsPatnerType = Convert.ToInt32(patner);



        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            string fname;
            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE" || HttpContext.Current.Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
            {
                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                fname = testfiles[testfiles.Length - 1];
            }
            else
            {
                fname = "Logo" + file.FileName;
            }

            fname = Path.Combine(context.Server.MapPath("~/Images/Patner/"), fname);
            file.SaveAs(fname);
}
}

这是我的jquery ajax代码:

 var Companyname = $.trim($("[id*=txtname]").val());
        var city = $.trim($("[id*=slctcity]").val());
        var email = $("[id*=txtemail]").val();
        var phone = $("[id*=txtphone]").val();
        var address = $.trim($("[id*=txtaddress]").val());
        var postalcode = $.trim($("[id*=txtpostalcode]").val());
        var contactpersonname = $.trim($("[id*=txtcontactpersonname]").val());
        var contactpersonalemail = $.trim($("[id*=txtcontactpersonemail]").val());
        var contactpersonphone = $("[id*=txtcontactpersonphone]").val();
        var passwords = $.trim($("[id*=txtpass]").val());
        var website = $.trim($("[id*=txtwebsite]").val());
        var companysize = $.trim($("[id*=slctcompanysize]").val());
        var companyrevenue = $.trim($("[id*=slctrevenue]").val());
        var selValue = $('input[name=radio]:checked').val();
        var patner = selValue;

        var fileUpload = $("#Upload").get(0);
        var files = fileUpload.files;
        var test = new FormData();
        for (var i = 0; i < files.length; i++) {
            test.append(files[i].name, files[i]);
        }
        test.append("Companyname", JSON.stringify(Companyname));
        test.append("city", JSON.stringify(city));
        test.append("email", JSON.stringify(email));
        test.append("phone", JSON.stringify(phone));
        test.append("address", JSON.stringify(address));
        test.append("postalcode", JSON.stringify(postalcode));
        test.append("contactpersonname", JSON.stringify(contactpersonname));
        test.append("contactpersonalemail", JSON.stringify(contactpersonalemail));
        test.append("contactpersonphone", JSON.stringify(contactpersonphone));
        test.append("passwords", JSON.stringify(passwords));
        test.append("website", JSON.stringify(website));
        test.append("companysize", JSON.stringify(companysize));
        test.append("companyrevenue", JSON.stringify(companyrevenue));
        test.append("patner", JSON.stringify(patner));

        var Messege = "";

        if (Companyname == '') {
            Messege = "Can not Blank Name";
        }

        if (Companyname == '') {
            Messege += "Can not Blank Last Name";
        }

        if (Messege.length == 0) {



            $.ajax({
                url: "RegisterHandler.ashx",
                type: "POST",
                contentType: false,
                processData: false,
                data: test,
                // dataType: "json",
                success: function (result) {
                    alert(result);
                },
                error: function (err) {
                    alert(err.statusText);
                }
            });


        }
    }

和context.request.params [“ companyname”];始终返回\“值” \ 我如何只获取“值”

1 个答案:

答案 0 :(得分:1)

var companyname = context.Request.Params["Companyname"].ToString().Replace(@"""", ""); 

-每个stackoverflow.com/questions/4673437/c-sharp-replace-characters