How can I strongly-type in what my View inherits from? - asp.net-mvc-2

CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'UTEPSA' (are you missing an assembly reference?)
Here is the view code that's trying to inherit:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<UTEPSA.Helpers.PaginatedList<Area>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th></th>
<th>
ID
</th>
<th>
IDJefe
</th>
<th>
Nombre
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.ID })%> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
</td>
<td>
<%: item.ID %>
</td>
<td>
<%: item.IDJefe %>
</td>
<td>
<%: item.Nombre %>
</td>
</tr>
<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
And a screenshot of my solution:
How can I correctly reference my PaginatedList? Since the view is inheriting from my thing I want, I'd expect to have strongly typed Model to work with. But intellisense isn't working so somethings wrong.

You must add the assembly to both the Project Reference list as well as the <system.web><compilation><assemblies> section of web.config.

Related

EJS syntax error while compiling ejs code

SyntaxError: Unexpected token ')' in C:\Users\hp\short.nner\views\index.ejs while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
at new AsyncFunction (<anonymous>)
at Template.compile (C:\Users\hp\short.nner\node_modules\ejs\lib\ejs.js:661:12)
at Object.compile (C:\Users\hp\short.nner\node_modules\ejs\lib\ejs.js:396:16)
at handleCache (C:\Users\hp\short.nner\node_modules\ejs\lib\ejs.js:233:18)
at tryHandleCache (C:\Users\hp\short.nner\node_modules\ejs\lib\ejs.js:272:16)
at View.exports.renderFile [as engine] (C:\Users\hp\short.nner\node_modules\ejs\lib\ejs.js:489:10)
at View.render (C:\Users\hp\short.nner\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\hp\short.nner\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\hp\short.nner\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\hp\short.nner\node_modules\express\lib\response.js:1012:7)
at C:\Users\hp\short.nner\server.js:49:17
at processTicksAndRejections (node:internal/process/task_queues:93:5)
Here's my code:
<%=Url.forEach(shortUrl => { %>
<tr>
<td>
<a href="<%= shortUrl.full %>">
<%= shortUrl.full %>
</a>
</td>
<td>
<a href="<%= shortUrl.short %>">localhost:3000/<%= shortUrl.short %></a
>
</td>
<td><%= shortUrl.clicks %></td>
<td><%= shortUrl.GivenEmail %>
</tr>
<% }) %>
I really don't know why I am getting this error please help.
I did give the Url as an option to my ejs but I get this strange error on running it.
I wasn't able to fully test your code yesterday because I was on mobile.
The problem is actually coming from the = in the beginning of <%=Url.forEach(shortUrl =>. Just remove it like this should work:
<% Url.forEach(shortUrl => { %>
<tr>
<td>
<a href="<%= shortUrl.full %>">
<%= shortUrl.full %>
</a>
</td>
<td>
<a href="<%= shortUrl.short %>">localhost:3000/<%= shortUrl.short %></a
>
</td>
<td><%= shortUrl.clicks %></td>
<td><%= shortUrl.GivenEmail %>
</tr>
<% }) %>
That should dismiss the error.

CanJS EJS table - rows appearing above the table

I am new to CanJS and was trying to learn via tutorials.. Once place where I got stuck was when I converted the todo tutorial code to use table instead of UL/LI combination.
May be I am making some very small mistake.. but could not find out the same for last 3 days.. Can someone help out?
The jsFiddle for the short version of todo tutorial (which now uses tables) is here where you can see the problems.
The crux of the code from ejs is here..
<script type='text/ejs' id='todosEjs'>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>status</th>
</tr>
</thead>
<tbody>
<!-- bind to changes in the todo list -->
<% this.each(function( todo ) { %>
<!-- add the todo to the element's data -->
<tr <%= (el) -> el.data('todo',todo) %>>
<td>
<input type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>>
</td>
<td><%= todo.attr('name') %></td>
<td><%= todo.attr('id') %></td>
</tr>
<% }) %>
</tbody>
</table>
</script>
Looking forward towards helping hands :-)
UPDATE: This was the result of a bug which is now fixed
It looks as if the comments are the problem here. Removing them makes it work, see the updated Fiddle:
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>status</th>
</tr>
<% this.each(function( todo ) { %>
<tr <%= (el) -> el.data('todo',todo) %>>
<td>
<input type="checkbox" <%= todo.attr('complete') ? 'checked' : '' %>>
</td>
<td><%= todo.attr('name') %></td>
<td><%= todo.attr('id') %></td>
</tr>
<% }) %>

Handling Multiple Submit Buttons

I Have a Strongly typed user control which i use for Searching a certain list of objects.
the following code shows the user control,
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PeercoreCRM.ViewModels.CustomerFilterViewModel>" %>
<div style="width: 100%;vertical-align:top;background-color:White">
<fieldset>
<legend>Criteria</legend>
<table cellspacing="0">
<tr>
<td style="width: 100px">
<div class="editor-label">
<%: Html.LabelFor(m => m.LeadName) %>
</div>
</td>
<td>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.LeadName) %>
</div>
</td>
</tr>
<tr>
<td style="width: 60px">
<div class="editor-label">
<%: Html.LabelFor(m => m.CustomerCode) %>
</div>
</td>
<td>
<div class="editor-field">
<%: Html.TextBoxFor(m => m.CustomerCode)%>
</div>
</td>
</tr>
<tr>
<td>
<input type="submit" name="btnSearch" value="Search" />
<input type="submit" name="btnCancel" value="Cancel" />
</td>
<td>
</td>
</tr>
</table>
</fieldset>
</div>
In the View, I show this user control conditionally using the following code snippet,
<% using (Html.BeginForm("CustomerList", "Customer", new { isFiltered = Model.FilterViewModel.IsFiltered }, FormMethod.Post))
{
%>
<% if (Model.FilterViewModel.IsVisible) Html.RenderPartial("ListFilterUserControl", Model.FilterViewModel); %>
<% } %>
I have put the Form in the rendering page as this control is used in other views and thereby calling other action methods in different controllers.
I have the following method signature in my controller action method,
[HttpPost]
public ActionResult CustomerList(CustomerFilterViewModel filterModel)
{
bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
Session["CurrentPageNumber"] = null;
return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
}
My problem is, with this implementation, how can i separately identify which button is clicked ("Search" or "Cancel") and to write the code depending on that.
To identify the button that has been passed in, you can group your buttons by adding a name attribute to them:
<input name="button" type="submit" value="Search" />
<input name="button" type="submit" value="Cancel" />
then, add a variable that is passed into your post method with the same name of the buttons (in this case "button") like this:
[HttpPost]
public ActionResult CustomerList(string button, CustomerFilterViewModel filterModel)
{
if(button.Equals("Search"))
{
bool filtered = filterModel.IsDirty? FilterCustomers(filterModel):false;
Session["CurrentPageNumber"] = null;
return RedirectToAction("CustomerList", new { isFiltered = filtered || filterModel.IsFiltered });
} else {
if(button.Equals("Cancel")) {
//perform cancel
}
}
}

Linq query Group By error in ASP.NET MVC 2

I am having a bit of a struggle using linq and returning a list while trying to group.
In my view, I have the following:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Log>>" %>
<table>
<tr>
<th>
ID
</th>
<th>
User
</th>
<th>
NumberDialed
</th>
<th>
Date
</th>
<th>
Time
</th>
<th> </th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%: item.ID %>
</td>
<td>
<%: item.UserName %>
</td>
<td>
<%: item.NumberDialed %>
</td>
<td>
<%: item.Date %>
</td>
<td>
<%: item.Time %>
<%} %>
</td>
</table>
and in my controller I have:
public ActionResult Tenant()
{
LogEntities db = new LogEntities();
var s = from logs in db.Logs
group logs by logs.UserName;
return View(s.ToList());
}
when I do a normal select it works:
public ActionResult Tenant()
{
using (LogEntities db = new LogEntities())
{
var s = from logs in db.Logs
orderby logs.UserName
select logs;
return View(s.ToList());
}
}
Why is using Group By not working and why it is so different? I want to be able to display the data by groups. How do I accomplish this?
Thanks :)
I think that in order to do a group you need to specify into what you need it to be grouped
var s = (from logs in db.Logs
group logs by logs.UserName into us
select us).ToList();
Then us.Key gives you the username by which it is grouped and us gives all the items that are grouped by this username.
Then you can do:
foreach(var userGroup in s){
var userName = us.Key;
var logs = us.ToList();
}

Renderpartial or renderaction

have an action that generates active vacancies. The code is below;
public ViewResult OpenVacancies()
{
var openvacancies = db.GetActiveVacancies();
return View(openvacancies);
}
I want to use this list on several pages so i guess the best thing to use is html.renderaction (please correct me if i am wrong here).
Please note that the view and .ascx control are in an Area.
I then created a view by right clicking inside the action and create a .ascx and a strongly typed view of Vacancy. I chose a view content of "List".
I then added this line to the required page;
Please note that the view and .ascx control are in an Area.
The error i got is;
The type or namespace name 'Vacancy' could not be found (are you missing a using directive or an assembly reference?)
the .ascx code is below;
>" %>
<table>
<tr>
<th></th>
<th>
VacancyID
</th>
<th>
JobTitle
</th>
<th>
PositionID
</th>
<th>
LocationID
</th>
<th>
JobDescription
</th>
<th>
JobConditions
</th>
<th>
Qualifications
</th>
<th>
RequiredSkills
</th>
<th>
Certifications
</th>
<th>
AdvertDate
</th>
<th>
AdvertExpiryDate
</th>
<th>
Status
</th>
<th>
StaffLevel
</th>
<th>
LineManagerEmail
</th>
<th>
ApprovalFlag
</th>
<th>
RequisitionDate
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.VacancyID }) %> |
<%= Html.ActionLink("Details", "Details", new { id=item.VacancyID })%> |
<%= Html.ActionLink("Delete", "Delete", new { id=item.VacancyID })%>
</td>
<td>
<%= Html.Encode(item.VacancyID) %>
</td>
<td>
<%= Html.Encode(item.JobTitle) %>
</td>
<td>
<%= Html.Encode(item.PositionID) %>
</td>
<td>
<%= Html.Encode(item.LocationID) %>
</td>
<td>
<%= Html.Encode(item.JobDescription) %>
</td>
<td>
<%= Html.Encode(item.JobConditions) %>
</td>
<td>
<%= Html.Encode(item.Qualifications) %>
</td>
<td>
<%= Html.Encode(item.RequiredSkills) %>
</td>
<td>
<%= Html.Encode(item.Certifications) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.AdvertDate)) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.AdvertExpiryDate)) %>
</td>
<td>
<%= Html.Encode(item.Status) %>
</td>
<td>
<%= Html.Encode(item.StaffLevel) %>
</td>
<td>
<%= Html.Encode(item.LineManagerEmail) %>
</td>
<td>
<%= Html.Encode(item.ApprovalFlag) %>
</td>
<td>
<%= Html.Encode(String.Format("{0:g}", item.RequisitionDate)) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
First, if you simply want to reuse your view on multiple pages, you should be using a shared partial view (place it in the Shared folder) with <% Html.RenderPartial("OpenVacancies"); %>.
Second, based on your code snippet, your partial view doesn't appear to be strongly typed. Instead of
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
you will want something like this:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Vacancy>>" %>