0

I am attempting to pass the data (type = string) from my MVC model to a javascript funcion but I keep getting an error "Uncaught SyntaxError: Unexpected token ILLEGAL". I believe this is caused by the newlines in the model data as it only occurs with multi-line strings.

The idea is for the user to press the "undo" button/link to reset the text area.

Image:

enter image description here

CSHTML:

@Html.TextAreaFor(model => Model.updates[i].body, new { @Class = "txtTextArea floatLeft", @Id = "Edit_" + i.ToString(), @style = "width: 70%;", @Rows = "4", @Cols = "30" })

<a class="textRight EditToLink" onclick="clearInput('[email protected]()', '@Html.Raw(Model.updates[i].body)')" id="[email protected]()" href="#" >undo</a>

Javascript Function:

function clearInput(input, val) {
    if (document.getElementById(input) != null) {
        if (val != null) {
            val.replace(/\r?\n/g, "\r\n");
            document.getElementById(input).value = val;
        }
        else {
            document.getElementById(input).value = "";
        }
    }
}

Similar Question: http://forums.asp.net/t/1741372.aspx?pass+model+parameter+to+javascript+function

Thanks is advance!

1 Answer 1

1

I do a replace(@"\n", "") on the string.

OR

Use JSON.NET JsonConvert.SerializeObject(model.data)

By the way, in CSS to display multiline stuff use this:

{
    white-space: pre-wrap;
}
Sign up to request clarification or add additional context in comments.

3 Comments

The .replace didn't work for me, but the JsonCovert.SerializeObject worked like a charm. Thanks a bunch! cheers
Yeah, no worries, I tend to just stuff my entire view model into JSON and have knockout take care of the rest!
@Racksickle see the update about css, if that is of any use! I've recently worked a lot with multiline stuff in MVC 4 and HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.