Using this code within an aspx file
<% if(storeid=1) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer></script>
<% } %>
<% else if(storeid=2) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-efgh.js" async defer></script>
<% } %>
<% else if(storeid=3) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-ijklmn.js" async defer></script>
<% } %>
<% else if(storeid=4) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-opqrs.js" async defer></script>
<% } %>
Compiling this gives me this error
Compiler Error Message: CS1525: Invalid expression term '<'
Source Error:
Line 62:
Line 63: // Specific Code test 17.4.2014
Line 64: <% if(storeid=1) { %>
Line 65: <script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer> </script>
Line 66: <% } %>
All of the <% and %> look ok. Where is it falling down?
It's storeid == 1, not storeid=1.
Replace this line:
<% if(storeid=1) { %>
With:
<% if( storeid == 1 ) { %>
By the way, this is true for all of the rest equality checks in the other lines of code.
Your storeid='1' is wrong and the rest of your else statement. it should be ==.
storeid == 1 and not storeid=1.
<% if(storeid=1) { %>
should be
<% if(storeid==1) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer></script>
<% } %>
<% else if(storeid==2) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-efgh.js" async defer></script>
<% } %>
<% else if(storeid==3) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-ijklmn.js" async defer></script>
<% } %>
<% else if(storeid==4) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-opqrs.js" async defer></script>
<% } %>
Related
Attempting to make a Questionnaire page for a client. It's a pretty large list of questions, so I've broken it up into sections and subsections to make it easier for the user to navigate. The problem seems to be that in order to build the page, I iterate over the sections/subsections/questions, and not the answers. Answers are stored in a dictionary, with the key being the ID number of the associated question.
The ViewModel passed to the view is UserQuestionnaireViewModel, QuestionnaireViewModel has a list of Sections, each of which has a list of SubSections, each of which has a list of Questions. The Answers Dictionary gets all Answers linked to the relevant user, indexed by the Question they're linked to. Missing Answers are populated here, the View breaks if they're missing.
Model
public class UserQuestionnaireViewModel {
ClientPortalDbContext db = new ClientPortalDbContext();
public QuestionnaireViewModel Questionnaire { get; set; }
public Dictionary<Int32, QuestionnaireAnswer> Answers { get; set; }
public UserQuestionnaireViewModel() { }
public UserQuestionnaireViewModel(Int32 userID) {
Questionnaire = new QuestionnaireViewModel();
Answers = new Dictionary<Int32, QuestionnaireAnswer>();
List<QuestionnaireAnswer> answerList = db.QuestionnaireAnswers.Where(a => a.UserID == userID).ToList();
foreach (QuestionnaireAnswer answer in answerList) {
Answers.Add(answer.QuestionID, answer);
}
foreach (QuestionnaireViewModel_Section section in Questionnaire.Sections) {
foreach (QuestionnaireViewModel_SubSection subsection in section.SubSections) {
foreach (QuestionnaireQuestion question in subsection.Questions) {
if (!Answers.ContainsKey(question.ID)) {
Answers.Add(question.ID, new QuestionnaireAnswer() {
QuestionID = question.ID,
UserID = userID,
AnswerBool = false,
AnswerText = ""
});
}}}}}}
The Section and SubSectionclasses contain a name string, and a List of whatever comes under them (SubSection and Question respectively). Question has an id int, string for question text, and bools for what fields to show.
View
<div class="content-wrapper">
<% using (Html.BeginForm("Save", "Questionnaire", new { input = Model.Answers })) { %>
<%: Html.AntiForgeryToken()%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<ul class="questionnaire-tabs">
<% Int32 tabIndex = 0; %>
<% foreach (clientportal.Models.QuestionnaireViewModel_Section section in Model.Questionnaire.Sections) { %>
<% tabIndex++; %>
<li id="<%: "tab-button-" + tabIndex.ToString()%>" data-tab="<%: "tab-" + tabIndex.ToString()%>">
<%: section.Name%>
</li>
<% } %>
</ul>
<div class="clear"></div>
<% tabIndex = 0; %>
<% foreach (clientportal.Models.QuestionnaireViewModel_Section section in Model.Questionnaire.Sections) { %>
<% tabIndex++; %>
<div class="questionnaire-tab-content" id="<%: "tab-" + tabIndex.ToString()%>">
<p><%: section.Heading%></p>
<% foreach (clientportal.Models.QuestionnaireViewModel_SubSection subsection in section.SubSections) { %>
<h4><%: subsection.Name%></h4>
<p><%: subsection.Heading%></p>
<div>
<table class="questionnaire-table">
<% foreach (clientportal.Models.QuestionnaireQuestion question in subsection.Questions) { %>
<% clientportal.Models.QuestionnaireAnswer answer = Model.Answers[question.ID]; %>
<% if (question.Enabled) { %>
<tr>
<td class="label-col">
<%: Html.Label(question.Text) %>
<%: Html.Hidden("Model.Answers.Index", question.ID) %>
<%: Html.Hidden("Model.Answers[" + question.ID + "].ID", answer.ID) %>
<%: Html.Hidden("Model.Answers[" + question.ID + "].QuestionID", answer.QuestionID) %>
<%: Html.Hidden("Model.Answers[" + question.ID + "].UserID", answer.UserID) %>
</td>
<td class="bool-col">
<% if (question.ShowCheckBox) { %>
<%: Html.CheckBox("Model.Answers[" + question.ID + "].AnswerBool", answer.AnswerBool) %>
<% } else { %>
<%: Html.Hidden("Model.Answers[" + question.ID + "].AnswerBool", answer.AnswerBool) %>
<% } %>
</td>
<td class="text-col">
<% if (question.ShowTextBox) { %>
<%: Html.TextBox("Model.Answers[" + question.ID + "].AnswerText", answer.AnswerText) %>
<% } else { %>
<%: Html.Hidden("Model.Answers[" + question.ID + "].AnswerText", answer.AnswerText) %>
<% } %>
</td>
</tr>
<% } %>
<% } %>
</table>
</div>
<% } %>
</div>
<% } %>
</fieldset>
<h4>Please ensure you have checked all sections of the Questionnaire before saving.</h4>
<input type="submit" value="Save Answers" />
<% } %>
</div>
On submitting the form, it goes through to the Controller fine (The Controller takes a Dictionary<Int32, QuestionnaireAnswer> for this Action), except the model sent through has 0 rows and fails ModelState.IsValid
Looking around SO has given me the idea that this is an issue with MVC and Dictionaries in general, or simply failure to bind the dictionary. Any answers in the Database are displayed correctly, it's simply the inability to get the contents of the page back to the controller that's preventing me moving onwards.
Don't use foreach loop. Use for loop.
One more thing always use Html.HiddenFor(model.xxxx) and for other controls also.
If you use for loop along with HiddenFor this will automatically create appropriate name for controls
If it's possible to separate out database operation from MVC model then it's best.
I have a partial view where I pass the model name to populate it, is there any way to pass model name as a parameter based on the controller action executed?
<div id = "Details" >
<% List<Freshmen>() = model based on controller action executed %>
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
Controller Action:
public ActionResult FreshmenDetails(string id)
{
DataContext Student = new DataContext();
var FreshmenDetails = Student.Freshmen.Where(a => Convert.ToInt64(a.Id) == Convert.ToInt64(id)).ToList();
return PartialView("FreshmenDetails", FreshmenDetails );
}
I have 3 more Actions each for SophomoreDetails(),JuniorDetails(),SeniorDetails()
Currently I am displaying Partial View like this:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
<% using (Html.BeginForm("SophomoreDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SophomoreDetails", new List<Sophomore>()); %>
<% } %>
<% using (Html.BeginForm("JuniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("JuniorDetails", new List<Junior>()); %>
<% } %>
<% using (Html.BeginForm("SeniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SeniorDetails", new List<Senior>()); %>
<% } %>
</div>
I want something like:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
I am having a hard time figuring out exactly what your after. That being said, have you tried using reflection?
return PartialView(Model.GetType().Name, new { // Partial View object });
Further more, you can use Model.GetType().Name to get the model object name and use it where you want.
That should work for you.
this code works fine in php to generate the custom scripts generators
<?php
$arrs = array("script.js","ui.js","jform.js");
foreach ($arrs as $var)
{
?>
<script type="text/javascript" src="<?php echo $var ?>" >
</script>
<?php
}
?>
this not compiling even whts the probem in my code the aspx but its not working
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% lst %>" />
<%
}
%>
You error is with script line change the line to below resolve your one issue..i tried in my environment
<script type="text/javascript" src="<% =lst %>" />
Full code will be :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% = lst %>" />
<%
}
%>
Try with following code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% = lst %>" />
<%
}
%>
try this
<%# Page Language="C#" ..
<%# Import Namespace="System.Collections.Generic" %>
<% var list = new List<string>() { "script.js", "ui.js", "jform.js" };
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<%= lst %>"></script>
<%
}
%>
I'm trying to create a pager in ASP.net and am running into an error.
Trying to do this in my markup:
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) { %>
<% if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"><%= pageNumber %></asp:LinkButton>
<% } %>
<% } %>
I am getting the following error:
Compiler Error Message: CS0103: The name 'pageNumber' does not exist in the current context
The error is happening on the LinkButton line. It works fine on the first if case...but for some reason my variable does not exist on the else case.
Does anyone have any idea why this is not compiling or how I could do the same thing differently.
It's been a while since I have done regular ASP.net and I am used to the MVC way.
You can't place <%= pageNumber %> inside a LinkButton.
Alternatively, use the Repeater control and the OnItemDataBound event to add logic such as pageNumber == this.PageIndex in the code behind.
<asp:Repeater ID="Pager" runat="server">
<ItemTemplate>
<asp:Literal ID="ltlGoToPage" runat="server" Visible="false"></asp:Literal>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click" Visible="false"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
You can toggle the visibilty of the controls in the OnItemDataBound event.
In the code behind, reference the controls and apply any logic:
var ltlGoToPage = (Literal)e.Item.FindControl("ltlGoToPage");
var lnkGoToPage = (Literal)e.Item.FindControl("lnkGoToPage");
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) {
if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber.ToString() %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"><%= pageNumber.ToString() %></asp:LinkButton>
<% }
} %>
This will help eliminate some of the markup. Also, called ToString() on page number as that's just common practice for me, but you might not want it that way.
This is not how you do things in asp.net web forms, you should do the loop in the code behind.
The error is caused by the fact that the problem <%= pageNumber %> is in the middle of a control.
Sorry about my previos post. I wanted to say the way I see it is to use Text property of the control.
This should work:
<div class="pager">
<% foreach(int pageNumber in this.PageCollection) { %>
<% if( pageNumber == this.PageIndex ) { %>
<span class="current"><%= pageNumber %></span>
<% } else { %>
<asp:LinkButton ID="lnkGoToPage" runat="server" OnClick="lnkGoToPage_Click"></asp:LinkButton>
<%
lnkGoToPage.Text = pageNumber.ToString();
} %>
<% } %>
Also, you should think about giving sdifferent IDs to the LinkButtons to make sure they are different. But, conceptually, the code above should work
I know you can do this
<%= Request.Form[0] %>
But how do you do something like this?
<% if(Request.Form[0]!=null)
echo "abc";
%>
<% if(Request.Form[0]!=null)
Response.Write("abc");
%>
Maybe you want to show the form value unless it doesn't exist then show "abc"?
<%= Request.Form[0] ?? "abc" %>
Or you could can use if blocks and normal markup inside.
<% if(Request.Form[0]!=null) { %>
<div class="echo">abc</div>
<% } %>