I have this code on a view:
#{
string x = "whatever";
}
and then I'm trying to define a helper function:
#helper MyHelper() {
<div class="whatever" #x>
</div>
}
but, apparently, #x "does not exist in the current context"... how do I access it?
Set it as parameter to MyHelper():
#helper MyHelper(string x) {...
Something useful here:
http://weblogs.asp.net/scottgu/asp-net-mvc-3-and-the-helper-syntax-within-razor
Related
I am using inline C to get some output. What I am trying to do is execute the inline C code and render its output to rails view via controller.
The code snippet for inline C is:
class Something
inline(:C) do |builder|
builder.c 'int test1() {
char array[20] = "-------Hello from Ruby!------\n";
printf("%s", array);
return array;
}'
end
end
Object is created in the controller of the rails application this way.
def index
something = Something.new
#something = something.test1
end
Now, when I try to store output value of the inline C, it just is not applicable while the output is displayed on the rails log(not on the log file). I am sure that the value displayed is via return array; of the Something class.
The index method has #something instance set which just stores some value like 4421355280 , but not the value. ie "-------Hello from Ruby!------"
I have an index to render the corresponding controller action.
<h1>Travel</h1>
<h3><%= #something %></h3>
How can I possibly store output value "-------Hello from Ruby!------" into #something?
Output of inline C
Thanks in advance.
Sorry guys, its seems that I had some problem on return type on C code. It should have been:
class Something
inline(:C) do |builder|
builder.c 'char * test1() {
char array[20] = "-------Hello from Ruby!------\n";
printf("%s", array);
return array;
}'
end
end
I have a partial view which I want to display a different type of HTML element based on a property of the model. The solution I've come up with is:
#if (Model.Element == "span")
{
<span>
Some stuff
</span>
}
else if (Model.Element == "p")
{
<p>
Some stuff
</p>
}
else
{
<div>
SomeStuff
</div>
}
This is obviously not very extensible. I'd like to write something like:
<#Model.Element>
Some stuff
</#Model.Element>
but it doesn't seem to be allowed. Is there any way to do something like this.
(I appreciate that this goes somewhat against the grain of MVC as the model and therefore the controller is specifying HTML. Actually, the element type comes from the parent view, so I think this is okay.)
Many thanks in advance.
#Html.Raw("<" + Model.Element + ">")
Some Stuff
#Html.Raw("</" + Model.Element + ">")
Is about as simple as possible. Though you could always use the TagBuilder:
#{
TagBuilder tb = new TagBuilder(Model.Element);
tb.InnerHtml = "Some Stuff";
#Html.Raw(tb.ToString())
}
I have converted my MVC3 application to MVC5, I had to change all views to razor. Having a challenge with a select list:
In ASPX view that works I am using the following:
<select id="Profession" name="Profession" style="width: 235px; background-color: #FFFFCC;">
<% List<string> allProfessions = ViewBag.AllProfessions;
string selectedProfession;
if (Model != null && !String.IsNullOrEmpty(Model.Profession))
selectedProfession = Model.Profession;
else
selectedProfession = allProfessions[0];
foreach (var aProfession in allProfessions)
{
string selectedTextMark = aProfession == selectedProfession ? " selected=\"selected\"" : String.Empty;
Response.Write(string.Format("<option value=\"{0}\" {1}>{2}</option>", aProfession, selectedTextMark, aProfession));
}%>
</select>
In Razor I am using:
<select id="Profession" name="Profession" style="width: 235px; background-color: #FFFFCC;">
#{List<string> allProfessions = ViewBag.AllProfessions;
string selectedProfession;}
#{if (Model != null && !String.IsNullOrEmpty(Model.Profession))
{selectedProfession = Model.Profession;}
else {selectedProfession = allProfessions[0];}
}
#foreach (var aProfession in allProfessions)
{
string selectedTextMark = aProfession == selectedProfession ?
"selected=\"selected\"" : String.Empty;
Response.Write(string.Format("<option value=\"{0}\" {1}>{2}</option>",
aProfession, selectedTextMark, aProfession));
}
</select>
The list shows up at the top of the page, I can't figure out where is the problem. Would appreciate your assistance.
Don't create your dropdown manually like that. Just use:
#Html.DropDownListFor(m => m.Profession, ViewBag.AllProfessions, new { style = "..." })
UPDATE
I tried your solution but got this error: Extension method cannot by dynamically dispatched
And, that's why I despise ViewBag. I apologize, as my answer was a little generic. Html.DropDownList requires the list of options parameter to be an IEnumerable<SelectListItem>. Since ViewBag is a dynamic, the types of its members cannot be ascertained, so you must cast explicitly:
(IEnumerable<SelectListItem>)ViewBag.AllProfessions
However, your AllProfessions is a simple array, so that cast won't work when the value gets inserted at run-time, but that can be easily fixed by casting it to a List<string> and then converting the items with a Select:
((List<string>)ViewBag.AllProfessions).Select(m => new SelectListItem { Value = m, Text = m })
There again, you see why dynamics are not that great, as that syntax is rather awful. The way you should be handling this type of stuff is to use your model or, preferably, view model to do what it should do: hold domain logic. Add a property to hold your list of profession choices:
public IEnumerable<SelectListItem> ProfessionChoices { get; set; }
And then, in your controller action, populate this list before rendering the view:
var model = new YourViewModel();
...
model.ProfessionChoices = repository.GetAllProfessions().Select(m => new SelectListItem { Value = m.Name, Text = m.Name });
return View(model);
repository.GetAllProfessions() is shorthand for whatever you're using as the source of your list of professions, and the Name property is shorthand for how you get at the text value of the profession: you'll need to change that appropriately to match your scenario.
Then in your view, you just need to do:
#Html.DropDownListFor(m => m.Profession, Model.ProfessionChoices)
Given that you don't have this infrastructure already set up, it may seem like a lot to do just for a drop down list, and that's a reasonable thing to think. However, working in this way will keep your view lean, make maintenance tons easier, and best of all, keep everything strongly-typed so that if there's an issue, you find out at compile-time instead of run-time.
I believe it's happening because of the Response.Write. Try this:
#Html.Raw(string.Format("<option value=\"{0}\" {1}>{2}</option>", aProfession,
selectedTextMark, aProfession))
I have been searching on how to return an integer value from controller to the gsp. I tried using this:
def test(){
def val = 1;
return val;
}
but it does not work. Please help.
Try
def test(){
def val = 1;
[val:val]
}
Add this code to your controller:
def test(){
def v = 10
render view:'test.gsp', model:[v:v]
}
Then, access the value from your test.gsp by "${v}"
Note that the render view is optional for test.gsp, i've written it only for demo purpose.. (i.e change the gsp filename).
Hope it helps.
The return statement is used to return values from services to controllers.
If you want to pass values from the controller to gsp, use:
[variableNameInGSP : valueToBeReturned]
I would like to create a helper to return a text one character below the other. Something like that:
S
A
M
P
L
E
The purpose of this helper is to have a table with a heading of only 1 character wide. As you can see on the picture below this is ugly:
Example below looks nice:
I would like something like:
#Html.DisplayVerticalFor(x => x.MyText)
Any idea?
Thanks.
You can create an HTML helper DisplayVertical. (I am not adding the steps of how to create html helpers). The DisplayVertical will first split your text in character array and wrap each character inside a div or any other block level element, which can be inserted desired place.The implementation of DisplayVerticalFor can be something like this :
public static MvcHtmlString DisplayVertical (this HtmlHelper helper, string text)
{
string OutputString = "";
string assembleString = "<div>{0}</div>";
char[] textarr = text.ToCharArray();
foreach( char a in textarr )
{
OutputString += String.Format(assembleString, a);
}
return new MvcHtmlString(OutputString);
}
and in razor it will placed like this :
<div class="style-to-adjust-width-n-height"> #Html.DisplayVertical ("Sample") </div>
If you want to pass on a lambda expression to this html helper like this #Html.DisplayVerticalFor(x => x.MyText) then you need to add lambda expression parsing code to find out the text.
Lastly, this is a very rough code however you can add "TagBuilder" etc to make it more neat and clean.