0

I need to apply a simple filter to any text that will be printed to page using razor command @.
in example see below codes:

public static class MyHelper
{
    public string MyFilter(this string txt)
    {
        return txt.Replace("foo", "bar");
    }
}

in this .cshtml view file

@{
    var text = "this is foo!!";
}
<div>@text</div>

I expect somehow to print this is bar!! instead of this is foo!!

mhesabi
  • 1,140
  • 3
  • 22
  • 48
  • 1
    You can create `HtmlHelper` for this purpose. – Adil Mammadov Aug 22 '16 at 10:09
  • 1
    Why would you do that in the view as opposed to in the controller (where it should be done)? –  Aug 22 '16 at 10:10
  • @StephenMuecke because the text that will be printed could be in a dataset or a variable or whatever possible and I think it's more simple to apply filter to razor response than run a function on each field of rows of a dataset. – mhesabi Aug 22 '16 at 10:15
  • 1
    Your logic should be done in the controller, not the view (and your cant just _apply a filter to razor response_) –  Aug 22 '16 at 10:18
  • How does your view know to use the `MyFilter` method? From what I see, you are simply creating a variable called `text` and then assigning it a string.. then displaying it in the view... You are not using the `MyFilter` method whatsoever in the view – Grizzly Aug 22 '16 at 12:40

2 Answers2

0

As @AdilMammadov said you can use HtmlHelper for that.

Simple C# class with static method:

using System;
namespace MvcApplication1.MyHelpers
{
    public class MyHelpers
    {
        public static string FooReplacer(string txt)
        {
            return txt.Replace("foo", "bar");
        }
    }
}

And use the helper in your view:

@using MvcApplication1
...
<p>@MyHelpers.FooReplacer("foo foo")</p> <!--returns <p>bar bar</p>-->
feeeper
  • 2,865
  • 4
  • 28
  • 42
  • In the view you have to specify the model.. why are you using a `using` statement in the View? That should be changed to `@model MvcApplication1.MyHelpers`.. then when you call the `FooReplacer` method you just have to do `Model.FooReplacer("foo foo")` – Grizzly Aug 22 '16 at 12:52
  • @BviLLe_Kid You're right that I can use `@model MvcApplication1.MyHelpers` but as I know my approach is also works fine http://stackoverflow.com/a/3244924/1821692 – feeeper Aug 22 '16 at 13:01
0

I believe you are close. The only problem is that your View doesn't know to use your filter because you didn't specify that within your View.

This should work:

Model

public static class MyHelper
{
    public string MyFilter(this string txt)
    {
        return txt.Replace("foo", "bar");
    }
}

View

@model AssemblyName.MyHelper

@{
    Layout = null;
    var text = Model.MyFilter("Let's go to the foo");
}
<div>@text</div>

// will display "Let's go to the bar"

I have created a dotnetfiddle for you, to show that this will work.

Hope this helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Please mark either one of the answers provided as accepted so that this question can be marked as answered! – Grizzly Aug 25 '16 at 13:26