Can I use this code directly in a ASP.NET MVC 3 Razor View instead of declaring variables? - asp.net-mvc

I have values in a resx file that I would like to output in my razor view. When I try it this way, it blows up.
<li>
#ResourceFacade<Global>.GetString("MenuLabelSupport");
</li>
but if I do this, it works as expected.
#{
var menuLabelSupport = ResourceFacade<Global>.GetString("MenuLabelSupport");
}
<li>
#menuLabelSupport
</li>
I would really like to use the first implementation. Perhaps my syntax isn't quite right? If anyone sees, that I'm doing something incorrectly, please let me know. Thank you so much for any tips or advice.

Try like this:
#(ResourceFacade<Global>.GetString("MenuLabelSupport"))

Related

How to add link title in Typo3

I'm actually absolutely new to Typo3 and need some help. I've got a site (6.1.5) with a main menu - but none of the links has a title:
<li>
page1
</li>
<li>
page2
</li>
In the backend I can use "Page Title", but nothing appears in the frontend.
Thanks in advance!
Dublay
We need to know more to help!
Have a look at the template section in the BE Menu. Look if you find there an Object named TMENU. Is so, this would be your solution:
ATagParams = title="{field:abstract // field:subtitle // field:title}"
ATagParams.insertData = 1
Like that first the abstract is taken, if empty the subtitle, if empty the title.
Have a look here: http://www.serious-cool.de/webdesign/typo3/dynamische-linktitel-im-menue/
I you cannot find anything alike, your template is propably rendered with fluid. That means you have somewhere in your system a navigation.html file you need to find and modify.
Since Typo3 is very dynamically developing there are more then one possibility to render a menu....

How to get the total items in an array(array length) in dust.js

I can't seem to figure out(I'm pretty sure that I'm over thinking or not thinking here) a way to get the length of an array in the following example. Can anyone please help me, or direct me to a good tutorial. https://github.com/linkedin/dustjs/wiki/Dust-Tutorial has mentioned the {#size} helper but does not have any examples.
<ul total-tems={#items.length/}>
{#items}
<li class="item-{$idx}">{.title}</li>
{/items}
</ul>
Thank you in advance.
Yep, I was clearly overlooking. I was not including ( require('dustjs-helpers'); ) in order to use
<ul total-tems="{#size key=items /}">
Hope this will help others out as well.
I don't know if is it a new feature of dustjs but now you can simply use {items.length} to access the array length with no need of helpers.

multiple Html.Attr on one tag in razor?

how do i use multiple Html.Attr on one tag as follows in a razor view? this doesnt work.
<tr #Html.Attr("style", "color: #FF3D0D;")
#Html.Atrr("data-item", Model.ItemNumber)>
I'm not familiar with the .Attr helper either, but just incase you copied the code directly from your view, there is a typo, you have #Html.Atrr which I'm guessing should be #Html.Attr for data-item
So, it has come to this..
#Html.Atrr("data-item", Model.ItemNumber)
- VS -
data-item="#Model.ItemNumber"
Just one word: don't. Your markup AND every single human being on earth will benefit from the later much straight forward syntax. The HtmlHelper Html.Atrr doesn't help at all, no value.
Also, the following works for me:
<body #Html.Raw("data-test1=\"1\"") #Html.Raw("date-test2=\"2\"")>
.. and renders:
<body data-test1="1" date-test2="2">
You probably should be using #Html.Attr and not #Html.Atrr... or, as others have pointed out, just writing the attributes explicitly.

Looking for repeater type functionality in ASP.Net MVC

I am used to using a Repeater control in conventional ASP.Net web projects. I see ASP.Net MVC doesn't have this sort of thing. What should I be using here?
EDIT:
In response to the question, what am I trying to do that I can't achieve in the foreach. I guess I am trying to get a alternating row style. Also, it just feels somewhat wrong to have stuff other than markup in the view. But maybe I will get over that as I work with it. Thanks for the answers.
The simplest thing to use is a foreach loop.
What are you trying to do?
EDIT:
<% bool odd = false;
foreach(var row in something) { %>
<tr class="<%= odd ? "OddRow" : "EvenRow" %>">
...
</tr>
<% odd = !odd; } %>
To add to SLaks response.
You could encapsulate your html into a Partial View.
Call <%= Html.Partial("ViewName", optional_ViewModel) %>.
This might feel closer to the repeater control. Where the PartialView would be sort of like your item template.
This approach also lends itself to code reuse very nicely.
If you miss a lot of the features of WebForms, maybe you just need a richer view engine? Might I suggest the Spark View Engine? Like WebForms, there's lots of functionality included so you don't have to keep rewriting the same stuff and/or write a bunch of your own helpers.
alternating row style can be achieved by css styles ... BTW - each one funcionality from web forms can be achieved in mvc ... in the end all is html, js and css
You could even create an HTML helper for this.

Designer-friendly views in Asp.Net MVC

I'm enjoying Asp.Net MVC and am looking to use it in an upcoming project. Part of the project, however, is an emphasis on being able to expose the project Views to designers for things like theming and so on. One problem I'm anticipating is that Asp.Net MVC views are rather developer-centric. I really don't want to have to educate designers on the intracies of <% vs. <%= let alone something like <% foreach ...
Take a typical MVC menu structure, for example.
<div id="menu">
<ul>
<li><%= Html.ActionLink("Home", "Index", "Main")%></li>
<li><%= Html.ActionLink("About", "About", "Main")%></li>
<li><% Html.RenderPartial("LogOnUserControl"); %></li>
</ul>
</div>
I'd much rather be able to tell designers to go with something like
<div id="menu">
<ul>
<li>{ActionLink "Home", "Index", "Main"}</li>
<li>{ActionLink "About", "About", "Main"}</li>
<li>{Partial "LogOnUserControl"}</li>
</ul>
</div>
Or
<div id="menu">
<ul>
<li><my:ActionLink text="Home" action="Index" controller="Main" /></li>
<li><my:ActionLink text="About" action="About" controller="Main" /></li>
<li><my:Partial name="LogOnUserControl" /></li>
</ul>
</div>
Yes, that last looks suspiciously like a raft of UserControls. Personally, I'm not a fan of actually using UserControls to do this if only because the rendering of those controls happens after pretty much everything else (as I understand it) and I'd prefer something that fits more in line with the MVC lifecycle. All I really need is a set of placeholders and a way to replace them with the relevant rendering.
So where's the best place to do so and what kind of trade-offs am I looking at here. I can imagine a couple of angles to come at this:
A custom ViewPage class where I can override something relevant. ViewPage.RenderView or ViewPage.FrameworkInitialize, maybe, but how you get at the text from there I don't know.
Create a custom TextWriter and override ViewPage.CreateHtmlTextWriter that I can then intercept the text output for replacing stuff. This is pretty late in the cycle, though, and will mess with other custom filtering if I'm not careful.
Create my own IView and ViewEngine classes. I didn't get far down this path before wondering if I was headed to a very bad place.
Custom UserControls that can mimic the functionality needed.
Opinions? Other options? Is my own ViewEngine my best option? My own ViewPage? Or are UserControl objects going to be adequate (please say no)?
Take a look at Spark. It's syntax is similar to your example.
As Charles Conway said, Spark is definitely way to go. Look at example. First, you create partial view in _ActionLink.spark with code:
<viewdata text="String" action="String" controller="String">
${ Html.ActionLink(controller, action, text) }
Then you use it like that in other views:
<ActionLink text="Home" action="Index" controller="Main" />
You just have to prepare partial views for your designers and then they an create view in prefered style. Isn't it easy?
EDIT:
Sorry, that was wrong. <ActionLink text="Home" action="Index" controller="Main" /> will not work, because Home, Index and Main are treates as variables. It may not be that easy to do it as you wish.
I have a similar requirement for a current project, except my 'designers' are more or less end users (people that know their way around html, but it's not their job) which leads to some additional challenges. My implementation is probably too specific for your needs but I'll quickly explain it lest it be useful to anyone else.
I wanted to totally avoid any kind of code in the views for the pages they would be designing so I decided to go with a templating system that does something similar to your point #2, but not at runtime. I am also using spark, although it's not for the benefit of the users as they won't be touching anything that looks like code.
Basically the users create a full html-only template that has placeholder tags for user controls and partial views. The users then upload the template through an interface that parses it and turns it into a spark view.
eg.
For a picture gallery, <gallery /> is parsed into something like !{Html.Gallery(Model)} or <use file="Gallery"/>. For a description field, <desc name="Kitchen" /> is parsed into !{Html.Description(Model, x => x.Descriptions.Kitchen)}. It also checks that "Kitchen" is in fact a property for the object/page that is being templated, or that the Model being passed in for a gallery actually contains a collection of images to avoid runtime errors.
Further properties can be specified in the tag to pass additional parameters to the parsed controls. If the control specified requires Javascript, then it is also included in the view. If there are any problems parsing, output is return specifying which placeholder is invalid and why.
While answered and accepted, I don't see the example of this: why don't you just use:
<li><a href='<%= Url.Action("Home", "Index")%>'>Main</a></li>
Similar in Spark. I prefer this to Html.ActionLink, Html.BeginForm, etc, whenever possible (almost always except for form controls where it's easier to have automatic name encoding with Html helpers).
And your designers do see the link.

Resources