This is a migrated thread and some comments may be shown as answers.

[Solved] Can't add new line character

3 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Manish
Top achievements
Rank 1
Manish asked on 29 Mar 2012, 02:04 AM
Hi,

I am using Telerik MVC Grid. One on the column in my grid shows comma separated list of states like below

ACT, NSW, QLD,VIC

I want to show each state in separate line like below
ACT
NSW
QLD
VIC

But I can't seem to achieve this trivial task.

My code looks like below.

columns.Template(e => FormatStates(e)).Title("States");

And the helper looks like below.

@helper FormatStates(MyModel item)
{
    @(item.States.Replace(",", "\n"))
}

I have also tried <br> and \r\n instead of \n but in vain.

Can somebody please show me how to add new line character.

Thanks in advance
Manish

3 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 29 Mar 2012, 10:43 AM
@helper FormatStates(MyModel item)
{
    @(item.States.Replace(",", "<br />"))
} 

this will work fine normally

Becareful that you use server binding and not ajax binding or this will not work, you should use client template instead
0
Accepted
Dadv
Top achievements
Rank 1
answered on 29 Mar 2012, 11:15 AM
I just try a simple example with this :

columns.Bound(e=>e.Names).Template(t=>t.Names.Replace(",","<br />")); 

and it work fine in server binding

For server binding AND ajax binding you can use jquery :

function onDataBound(e) {
    $.each($('.format'), function (index, cell) {
        $(this).html($(this).html().replace(/,/g, '<br />'));
    });
}

 columns.Bound(f => f.FileName).HtmlAttributes(new {@class="format"}).Width(200);        

 .ClientEvents(events => events.OnDataBound("onDataBound")) 
0
Manish
Top achievements
Rank 1
answered on 29 Mar 2012, 11:58 PM
Hi Dadv,

Thanks for the quick response. I tried below and it worked like charm.

columns.Bound(e=>e.Names).Template(t=>t.Names.Replace(",","<br />"));


But for some unknown reason it doesn't work if I use @helper. For example, below code won't work. it will display <br /> as literal  text on browser.

columns.Template(e => FormatStates(e)).Title("States");

@helper FormatStates(MyModel item)
{
    @(item.States.Replace(",", "<br />"))
}
Tags
Grid
Asked by
Manish
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Manish
Top achievements
Rank 1
Share this question
or