public override object BindForModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var request = controllerContext.HttpContext.Request; StreamReader r = new StreamReader(request.InputStream); string s = r.ReadToEnd(); var queryParams = HttpUtility.ParseQueryString(s); AdInfo a = new AdInfo(); a.InternalId = Int32.Parse(queryParams["internalId"]); a.Name = queryParams["name"].Replace("amp;", "&"); a.Description = queryParams["description"].Replace("amp;", "&"); a.Radius = Int32.Parse(queryParams["radius"]); a.Lat = double.Parse(queryParams["lat"]); a.Lon = double.Parse(queryParams["lon"]); return a; }
public ActionResult CreatePost(AdInfo adInfo) { User user = Session[SessionVars.User] as User; string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; NpgsqlConnection conn = new NpgsqlConnection(connectionString); string pointToGeography = string.Format(@"ST_GeomFromText('POINT({0} {1})', 4326)", adInfo.Lon, adInfo.Lat); NpgsqlCommand command = new NpgsqlCommand(string.Format(@"INSERT INTO ads (client, name, description, radius, longitude, latitude, the_geog ) VALUES ( @client, @name, @description, @radius, @longitude, @latitude, {0} )", pointToGeography)); command.Parameters.Add("@client", NpgsqlDbType.Integer).Value = user.InternalId; command.Parameters.Add("@name", NpgsqlDbType.Varchar, 100).Value = adInfo.Name; command.Parameters.Add("@description", NpgsqlDbType.Varchar, 100).Value = adInfo.Description; command.Parameters.Add("@radius", NpgsqlDbType.Integer).Value = adInfo.Radius; command.Parameters.Add("@longitude", NpgsqlDbType.Double).Value = adInfo.Lon; command.Parameters.Add("@latitude", NpgsqlDbType.Double).Value = adInfo.Lat; command.Connection = conn; try { conn.Open(); command.ExecuteNonQuery(); return new WebResult(new ResultInfo() { GreatSuccess = true }); } catch (NpgsqlException e) { return new WebResult(new ResultInfo() { GreatSuccess = false, Message = e.Message }); } finally { conn.Close(); } }