Syntax Issue- Razor MVC4 - syntax

I'm trying to display a partial view using a custom helper method. The only problem is, I can't get past this syntax issue. The model set for this cshtml file is an IEnumerable collection of models I've defined as Operation. I'm sure this is an easy fix. Here's an example of what I'm seeing:
I have this block of code:
#using(Html.BeginForm()) {
<div id="editor_rows">
#foreach (var item in Model){
Html.RenderPartial("OperationEditorRow", item);
}
</div>
}
This gives me the following error at runtime:
Unexpected "{" after "#" character. Once inside the body of a code block (#if {}, #{}, etc.) you do not need to use "#{" to switch to code.
But if I remove the # sign in front of the foreach statement, everything is interpreted as plain text. So I tried placing an # in front of Html as follows:
#using(Html.BeginForm()) {
<div id="editor_rows">
#foreach (var item in Model){
#Html.RenderPartial("OperationEditorRow", item);
}
</div>
}
This comes back with a compilation error that says:
Cannot implicitly convert type void to object
If I run the project from here, I get a runtime error that says:
The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
I'm assuming this is related to the previous error. If anybody has a suggestion for me, please help.

Problem solved. I worked on this with a coworker. It turns out the error refferring to the write method pointed to a problem inside my partial view. I was using #{} around a block of code inside of there, which was most likely throwing the other syntax errors also. Thanks for the responses.

Add {}'s around your render call like #{RenderPartial...}

Related

Possible breaking change in MVC4 Razor that can be fixed with "#:#"

I recently upgraded my website from ASP.NET MVC3 (Razor) to MVC4 (Razor2), and in doing so found what seemed like a breaking change in the Razor view engine.
The scenario (greatly simplified) is shown below.
#model IEnumerable<string>
#{ Layout = null; }
<!DOCTYPE html>
<html>
<body>
<div>
#foreach (var x in Model)
{
#string.Format("Foo bar: {0}", x) // Errors in MVC4/Razor2
}
</div>
</body>
</html>
This works fine in MVC3/Razor, however in MVC4/Razor2 the string.Format line results in an error of:
Unexpected "string" keyword after "#" character. Once inside code, you do not need to prefix constructs like "string" with "#".
If you remove the #, the view engine then demands that you terminate the string.Format line with a semicolon. However, ReSharper then warns (rightly so):
Return value of pure method is not used.
The two fixes I've found for this are either to use <text>:
<text>#string.Format("The value {0}", x)</text>
Or a more curious approach using #:#:
#:#string.Format("The value {0}", x)
Is this a known and documented change in the Razor view engine?
Seems like a bug. It works with String:
#foreach (var x in Model)
{
#String.Format("Foo bar: {0}", x)
}
This is indeed a bug we decided not to fix, note that the syntax is incorrect as there is no transition between C# and markup in this case.
I understand that resharper shows a warning here but I believe the warning is wrong.
Here is the bug for future reference
https://aspnetwebstack.codeplex.com/workitem/458

Razor syntax issue with RenderPartial (CS1501)

The RenderAction is working just fine but as soon as I surround it with a if statement I get a compile error:
#if (#Model.IsConfigurationAllow)
{
#{ Html.RenderAction("Save"); } // CS1501: No overload for method 'Write' takes 0 arguments
}
More general question where can I found the grammar for the Razor view syntax?
Html.RenderAction renders the HTML directly into the response, so you cant call it in a code block.
The counterpart Html.Action returns a string with the results.
See http://haacked.com/archive/2009/11/17/aspnetmvc2-render-action.aspx
Did you try this?
#if (#Model.IsConfigurationAllow)
{
<text>#{ Html.RenderAction("Save"); }</text>
}
There are a few below (more can be found just by googling);
www.w3schools.com
A quick Reference
Introduction of Using Razor Syntax

Sending a value through render() in grails controller

This is probably one very simple question to answer, but I can't output the variable I am trying to send to a view through a controller in the Grails Framework.
render(view: 'parseerror', error: it)
That's my code in my controller, it renders the view correctly but as soon as I am trying to call the variable through ${error} it outputs nothing. It should output a string since when I print the iterator, the console output: The example string
Thanks for helping me out :)
The map key is "errors", not "error", but I belive, you want to achieve something similar to this code:
render( view: 'parseerror', model: [ 'error': error ] )

HtmlTags within Resx displayed in #Html.ValidationMessageFor

I have the following doubt. I am considering the option to have html tags within my resx texts for localization.
When I put the resources directly I can resort to:
#Html.Raw(#Resources.ResourcesFM.Error_Email)
and it works as expected.
The problem is when the resource is being called by a validation message from an htmlhelper:
#Html.ValidationMessageFor(model => model.Email)
Got from an attibute:
[DataType(DataType.EmailAddress,
ErrorMessageResourceType = typeof(ResourcesFM),
ErrorMessageResourceName = "ErrorMailIncorr")]
What I am trying...
#Html.Raw(Html.ValidationMessageFor(model => model.Email))
I do not know how to get the same result as when using #html.Raw as the output from the helper is a MvcHtmlString...
Thanks
Try this:
View:
#Html.Raw(Server.HtmlDecode(#Html.ValidationMessageFor(m => m.UserName).ToString()))
Controller Action:
ModelState.AddModelError("UserName", "This is a link <a href='http://example.com'>Google Home</a>");
Html.ValidationMessageFor html-encodes the message. But you should be able to simply call HttpUtility.HtmlDecode() on the result. Even though the result contains html tags and whatnot, the decode will simply no-op on that part of the string.
So if `Html.ValidationMessageFor(...)' returns
<span><div>This is in a div</div<></span>
Then HttpUtility.HtmlDecode(Html.ValidationMessageFor(...).ToString()) will give you
<span><div>This is in a div</div></span>
It's not pretty, but it works. Your alternative is to recreate your own Validation helper version that never encodes the message.
For Localization, you may use String.Format and choose appropriate placeholder for the link
ModelState.AddModelError("UserName", String.Format("This is a link {0}", "<a href='http://example.com'>Appropriate String From a Resource</a>"));

Why is my custom HTML Helper result getting html encoded?

I've got the following custom html helper in asp.net mvc 3
public static string RegisterJS(this HtmlHelper helper, ScriptLibrary scriptLib)
{
return "<script type=\"text/javascript\"></script>\r\n";
}
The problem is that the result is getting html encoded like so (I had to add spaces to get so to show the result properly:
<script type="text/javascript"></script>
This obviously isn't much help to me.. Nothing I've read says anything about this.. any thoughts on how I can get my real result back?
You're calling the helper in a Razor # block or an ASPX <%: %> block.
These constructs automatically escape their output.
You need to change the helper to return an HtmlString, which will not be escaped:
return new HtmlString("<script ...");

Resources