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
Related
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
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 ] )
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...}
I've tried getting the following helper to work...but I'm scratching my head.
#helper SimpleHelper()
{
string message;
message="<b>Hello</b>";
#message
}
The text comes out improperly encoded. With the lt; and gt; instead of the html tags.
Neither WriteLiteral(message) or #:message work.
Use the Html.Raw() helper in Razor to display that as an un-encoded string.
Here's the Razor quick-reference that Phil Haack put together recently.
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 ...");