What I am trying to do is to be able to write javascript blended with razor styled markup and methods inside .cshtml file and send this to a separate method for usage later on.
My .cshtml looks something like this:
@{SomeClass.SaveForLater(@<script type="text/javascript">window.alert('@Model.SomeParamter')}</script>);
And inside the class SomeClass:
public static void SaveForLater(HtmlString str) {
// will be using str.ToString() here and save the string output for use later on.
}
But what I receive is this error message:
CS1660: Cannot convert lambda expression to type ‘System.Web.HtmlString’ because it is not a delegate type
Am I using the wrong type for the argument or do I need to rethink the whole concept?
Solution
Thanks to SLaks below I ended up doing this:
public static void SaveForLater<T>(Func<T, HelperResult> template, dynamic model)
{
// template(model).ToHtmlString()
}
Using it like this:
@{SomeClass.SaveForLater<SomeModel>(
@<script type="text/javascript">window.alert('@Model.SomeParamter')</script>,
Model
);
You’re trying to take an inline helper.
You need to accept a
Func<Something, HelperResult>.