I was wondering if it's possible to render an Html Helper in a View inside a codeblock. So instead of:
<% = Html.TextBox("sometextbox", "somethingelse") %>
I want to do:
<%
switch(SomeParameter)
{
case "blah":
Html.TextBox("sometextbox", "somethingelse")
break;
}
%>
And have this render. Of course as it is, it wont render, so is there a way to programically decide if a textbox can be added without having to have a million delimiters in the page to accomplish this?
Thanks in advance!
<%
switch(SomeParameter)
{
case "blah":
%><%=Html.TextBox("sometextbox", "somethingelse")%><%
break;
}
%>
<%= %> is just a shorthand notation for Response.Write() though so the following should work too.
<%
switch(SomeParameter)
{
case "blah":
Response.Write(Html.TextBox("sometextbox", "somethingelse"));
break;
}
%>
All the HtmlHelpers return a string and don't output to the response stream directly by design.
Is this what your looking for?
<% switch (SomeParameter)
{
case "blah": %>
<%= Html.TextBox("sometextbox", "somethingelse") %>
<% break;
} %>
Related
I have loop in EJS and I want to set css based on my data:
<% dummies.forEach(function(dummy) { %>
<div class="panel panel-???">
</div>
<% }) %>
my data:
var dummies = [{
type: "funny",
sure: "Yes" // this should generate panel-success
}, {
type: "funny", // this should generate panel-a
sure: "No"
}, {
type: "happy", // this should generate panel-b
sure: "No",
}];
So whenever sure attribute is Yes, it generates panel-success. Otherwise, it generates css based on type attribute.
Please help.
First create object like this:
<%
let cssClass = {
funny: {
Yes: "success",
No: "a"
},
happy: {
No: "b"
}
}
%>
Then you should just output value in template:
<% dummies.forEach(function(dummy) { %>
<div class="panel panel-<%= cssClass[dummy.type][dummy.sure] %>">
</div>
<% }) %>
Try using this code...
<% dummies.forEach(function(dummy) { %>
<div class="panel panel-
<% if (dummy.sure==="yes") { %>
success
<% else %>
<%= dummy.type %>
>
</div>
<% }) %>
You can just wrap your logic inside the EJS operators. It looks a little messy but it is what it is. Depending on how your server side is set up, you could also pass in parameters from the server side, instead of doing the logic on the client side.
The best way would depend on where the heavy lifting is done as far as trips to the database and any other logic going on.
I think this code will help :
<a class="<%= 1==2 ? 'active' : '' %>" href="#"></a>
I am trying to implement if else in ASP file. But it is giving 500 internal server error.
Earlier code was
<title><%=title_tag%></title>
But I've tried to do
<%= if(meta_keywords_tag.IndexOf("MI")!= -1){ %>
<title>MI</title>
<% }
else { %>
<title><%=title_tag%></title>
<% } %>
When I remove this, It works fine. I'm unable to make it work. Thanks.
After commenting, it seems you have a typo at
<%= if(meta_keywords_tag.IndexOf("MI")!= -1){ %>
goes to
<% if(meta_keywords_tag.IndexOf("MI")!= -1){ %>
<title>MI</title>
<% }
else { %>
<title><%=title_tag%></title>
<% } %>
I found a solution to this
<title><%If 2 > 1 Then%>
MI
<% Else %>
<%=title_tag%>
<%End If %>
</title>
This works like a charm, But the issue is that I want to use a code behind variable in if statement
<%If ("" <%=title_tag%>"".ToString().Contains("MI") = True) Then%>
It is not working.
I want to have an "dynamic" table ascx:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<% foreach (var element in Model.Elements) { %>
<%= Html.DisplayFor(m => element) %>
<% } %>
with model:
public class TableViewModel<ElementType> {
public List<ElementType> Elements {get;set;}
}
first strangely enough I cannot write something like this in ascx directives:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl>" %>
then the second problem - I cannot use DisplayFor function extension, because compiler cannot deduce HtmlHelper<> generic param, because the ascx model is dynamic. That is why I had to replace the:
<%= Html.DisplayFor(m => element) %>
with the:
<% RenderPartialExtensions.RenderPartial(Html, "Table", element); %>
So my questions are am I doing something wrong, or if I'm not what is the difference between DisplayFor and RenderPartial in terms of performance?
Thanks in advance.
May be you could try to use just Display?
RenderPartial just renders partial view. DisplayFor uses UIHint or DisplayTemplate from Shared views for rendering.
why do I see the CS1026 error:) expected at the line below ?
<%=Html.BeginForm("AddAdvertisement", "Advertisement"){%> //here
hello
<%} %>
I think you want this instead (docs):
<% using(Html.BeginForm("AddAdvertisement", "Advertisement")) { %> //here
hello
<% } %>
I insert into asp.net mvc views C# logic that manages layout like the following:
<% if (Model.People.Count > 0 ) { %>
<% foreach (var person in Model.People) { %>
...
<% }} else { %>
<span class="error">Sorry, no people</span>
<%} %>
I try to minimize <% %> code placing "{" symbol on the same line as it's condition (java-style). Html layout looks more clear to me after that.
Do you apply C# formatting rules to <% %> html injections "}" should be on a new line or manage layout in different way?
Thank you in advance!
Its totally up to you, whatever you find is more readable and maintainable.
The less inline server blocks you have the better though (in terms of preventing run-time code compilation errors).