0

Element styles are sent from browser to ASP.NET MVC4 Web API using code below. For color properties json data contains strings:

backgroundcolor: "rgba(0, 0, 0, 0)"
bordercolor: "rgb(211, 211, 211)"
color: "rgb(51, 51, 51)"

API controller in server should parse those strings to get color values as integers.

How to send this data in parsed form, every rgb component value as separate integer property ?

Or how to parse rgb() strings in MVC API controller to get rgb values as integers ?

Or is there some simpler way to send element styles to API controller and parse them easly in browser or in MVC controller ?

function designerSave() {
    var elementsToSend = [];
    $(".designer-element").each(function () {
        var $this = $(this),
            moot = $this.css(["width", "height", "text-align", "font-family", "font-size", "font-weight",
                "font-style", "color", "border-color", "background-color"]),
            element = {
                id: $this.attr('id'),
                vpos: Math.round($this.position().top, 2),
                hpos: Math.round($this.position().left, 2),
                width: parseFloat(moot.width),
                height: parseFloat(moot.height),
                textalign: moot["text-align"],
                fontfamily: moot["font-family"],
                fontsize: moot["font-size"],
                fontweight: moot["font-weight"],
                fontstyle: moot["font-style"],
                color: moot.color,
                bordercolor: moot["border-color"],
                backgroundcolor: moot["background-color"]
            };
        elementsToSend.push(element);
    });
    $.ajax("api/Raport",
    {
        contentType: "application/json",
        data: JSON.stringify({ elements: elementsToSend }),
        type: 'POST'
    });
}
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • You will need to parse those strings yourself. Write a little helper function. There is no shortcut – charlietfl Jan 24 '16 at 16:36
  • Should those parsed in javascript in browser or in C# API controller in server? Maybe there is some javascript object or jquery function which exposes those values as integer properties ? If not, where to find javascript or C# helper function which parses those ? – Andrus Jan 24 '16 at 17:13
  • nothing exposes them as integers. The browser only returns them as string as you are already seeing. Use a regex to parse them anywhere you want ...client or server – charlietfl Jan 24 '16 at 17:15
  • What are you needing to do with them at higher level use case? – charlietfl Jan 24 '16 at 17:17
  • Values are stored in database as FoxPro frx table. This table has separate columns for every color: http://mattslay.com/foxpro-report-frx-table-structure/ – Andrus Jan 24 '16 at 18:22
  • @charlietfl I havent never used rexex. Where to find sample to extract r,g,b values from those strings in javascript ? – Andrus Jan 24 '16 at 18:35
  • do a search to look for regex that takes values from inside braces. Should find lots of results. Don't search based on rgb ...that part will be irrelevant – charlietfl Jan 24 '16 at 18:38
  • What on earth does that Foxpro do? Seems overly complex just to store data to replicate html – charlietfl Jan 24 '16 at 18:39
  • That FoxPro uses this data to create reports. I posted related question in http://stackoverflow.com/questions/34980574/how-to-extract-color-values-from-string-in-javascript – Andrus Jan 24 '16 at 19:43

0 Answers0