PagedList MVC 3 - Page Numbers not showing - asp.net-mvc

<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%# Import Namespace="PagedList.Mvc" %>
<%# Import Namespace="PagedList" %>
<!DOCTYPE html>`enter code here`
<html>
<head runat="server">
<title>ViewPage1</title>
<link href="../../Public/css/PagedList.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<table class="tableEmptyStyle">
<tr>
</tr>
</table>
<table>
<tr>
<th>
Code
</th>
<th>
Name
</th>
</tr>
<%
foreach (var item in ViewBag.OnePageDivisions)
{ %>
<tr>
<td>
<%: item.Code %>
</td>
<td>
<%: item.Name %>
</td>
</tr>
<% Html.PagedListPager((IPagedList)ViewBag.OnePageDivisions, page => Url.Action("Index", new { page = page }), PagedListRenderOptions.PageNumbersOnly); %>
<% } %>
</table>
Then from the controller I am getting the list and
ViewBag.OnePageDivisions = allList
I am able to get the list and display it,
the problem is the page numbers below are not showing.
The CSS, "PagedList.css" was taken from the package. Any help is appreciated
I am using MVC 3, PagedList 1.15.0.0 and PagedList.Mvc 3.18.0.0

Change
<% Html.PagedListPager((IPagedList)ViewBag.OnePageDivisions, page => Url.Action("Index", new { page = page }), PagedListRenderOptions.PageNumbersOnly); %>
To
<%: Html.PagedListPager((IPagedList)ViewBag.OnePageDivisions, page => Url.Action("Index", new { page = page }), PagedListRenderOptions.PageNumbersOnly) %>

Related

The Server block is not well formed

I am trying to add aspx view in MVC instead of Razor View.
I'm getting error at line <%# Html.DisplayNameFor(model => model.Name) %>
Parser Error Message: The server block is not well formed. <--- In browser
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="WebApplication1.Views.EmployeesPart25MVC.List" %> <%# Import Namespace="WebApplication1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
<title></title> </head> <body>
<form id="form1" runat="server">
<div>
<h2>
Gomathi
</h2>
<table class="table">
<tr>
<th>
<%# Html.DisplayNameFor(model => model.Name) %>
</th>
<th>
<%# Html.DisplayNameFor(model => model.Gender) %>
</th>
<th>
<%# Html.DisplayNameFor(model => model.City) %>
</th>
<th>
<%# Html.DisplayNameFor(model => model.DepartmentsPart25MVC.Name) %>
</th>
<th></th>
</tr>
<%#foreach (var item in Model)
{ %>
<tr>
<td>
<%# Html.DisplayFor(modelItem => item.Name) %>
</td>
<td>
<%# Html.DisplayFor(modelItem => item.Gender) %>
</td>
<td>
<%# Html.DisplayFor(modelItem => item.City) %>
</td>
<td>
<%# Html.DisplayFor(modelItem => item.DepartmentsPart25MVC.Name) %>
</td>
<td>
<%# Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
Html.ActionLink("Details", "Details", new { id=item.EmployeeID }) |
Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID }) %>
</td>
</tr>
<%# } %>
</table>
</div>
</form> </body>
</html>
In aspx engine <%# %> is only used for page directives, and cannot be used elsewhere.
For all the HTML output you would normally use <%= %>:
<%= Html.DisplayNameFor(model => model.Name) %>
and for code blocks just <% %>:
<% foreach (var item in Model)
{ %>

MVC Error: model item of type 'System.Collections.Generic.IEnumerable`1 required

I'm doing some modifications to a system, and I've run into an exception that I was hoping someone could help with?? The error I'm getting is the following:
Exception Details:
System.InvalidOperationException: The
model item passed into the dictionary
is of type
'System.Collections.Generic.List1[System.String]',
but this dictionary requires a model
item of type
'System.Collections.Generic.IEnumerable1[MyApp.Data.aspnet_Users]'.
Basically what I want to do is display a list of Inactive users. Here is the controller I've written for that:
public ActionResult InactiveUsers()
{
using (ModelContainer ctn = new ModelContainer())
{
aspnet_Users users = new aspnet_Users();
DateTime duration = DateTime.Now.AddDays(-3);
var inactive = from usrs in ctn.aspnet_Users
where usrs.LastActivityDate <= duration
orderby usrs.LastActivityDate ascending
select usrs.UserName;
return View(inactive.ToList());
}
}
From here then I added a partial view. Here is the code for it if anyone is interested. Just the usual Add View --> List.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>" %>
<table>
<tr>
<th></th>
<th>
ApplicationId
</th>
<th>
UserId
</th>
<th>
UserName
</th>
<th>
LoweredUserName
</th>
<th>
MobileAlias
</th>
<th>
IsAnonymous
</th>
<th>
LastActivityDate
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.ActionLink("Edit", "Edit", new { id=item.UserId }) %> |
<%: Html.ActionLink("Details", "Details", new { id=item.UserId })%> |
<%: Html.ActionLink("Delete", "Delete", new { id=item.UserId })%>
</td>
<td>
<%: item.ApplicationId %>
</td>
<td>
<%: item.UserId %>
</td>
<td>
<%: item.UserName %>
</td>
<td>
<%: item.LoweredUserName %>
</td>
<td>
<%: item.MobileAlias %>
</td>
<td>
<%: item.IsAnonymous %>
</td>
<td>
<%: String.Format("{0:g}", item.LastActivityDate) %>
</td>
</tr>
<% } %>
</table>
<p>
<%: Html.ActionLink("Create New", "Create") %>
</p>
And for completeness here is the Index page where I render the partial:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Administrator.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% Html.RenderAction("InactiveUsers"); %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
If anyone has any advice I'd be very grateful :)
Hi i think its because in your partial view type is System.Web.Mvc.ViewUserControl<IEnumerable<MyApp.Data.aspnet_Users>>
but you select user names
select usrs.UserName;
return View(inactive.ToList());
Try change it to select usrs; but not usrs.UserName

MVC: Form not rendering in partial view

I have a form with tabs. Subordinate to each tab is a partial view.
When the user submits to save changes, I want to call a method for that partial view, so for the partial view I put in <% using (Html.BeginForm(...) %>. But this gets ignored and clicking submit will call the method associated with the container view instead. Why is this, and how do I get this to work?
The View looks like;
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Employee.Master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.HolidayRequestViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
HolidayRequest
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="EmployeeContent" runat="server">
<% using (Html.BeginForm()) {%>
<%: Html.AntiForgeryToken() %>
<h2>Holiday Request</h2>
<p>You have <%: Html.DisplayFor(model => model.DaysAvailableThisYear) %> days left annual leave for this year
and <%: Html.DisplayFor(model => model.DaysAvailableNextYear) %> days left for next year.</p>
<p>If your request is approved and exceeds the number of days left, then those extra days will not be paid.</p>
<div id="tabs">
<ul>
<li>Approver</li>
<li>Single Date</li>
<li>Date Range</li>
</ul>
<div id="tabs-1">
<% Html.RenderPartial("GetApproverTab", Model); %>
</div>
<div id="tabs-2">
<% Html.RenderPartial("GetSingleDateTab", Model); %>
</div>
<div id="tabs-3">
<% Html.RenderPartial("GetDateRangeTab", Model); %>
</div>
</div>
<%: Html.HiddenFor(x => x.EmployeeId) %>
<% } %>
</asp:Content>
The partial view looks like;
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.HolidayRequestViewModel>" %>
<% using (Html.BeginForm("GetSingleDateTab", "Employee", FormMethod.Post, new { id = "frmSingleDate" }))
{ %>
<p>Enter your day or part day of Annual Leave here.</p>
<table>
<tr>
<td align="right">Date:</td>
<td><%: Html.EditorFor(x => x.SingleDate) %></td>
</tr>
<tr>
<td align="right">Morning Only:</td>
<td><%: Html.CheckBoxFor(x => x.MorningOnlyFlag) %></td>
</tr>
<tr>
<td align="right">Afternoon Only:</td>
<td><%: Html.CheckBoxFor(x => x.MorningOnlyFlag) %></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save" /></td>
</tr>
</table>
<% } %>
and the controller looks like;
#region Employee Holiday Request
public ActionResult HolidayRequest()
{
return View(new HolidayRequestViewModel(Employee.GetLoggedInUser().EmployeeId));
}
[HttpPost] // This is the method that gets executed
public ActionResult HolidayRequest(HolidayRequestViewModel hrvm)
{
if (ModelState.IsValid)
{
hrvm.Update();
return View(new HolidayRequestViewModel(hrvm.EmployeeId));
}
return View(hrvm);
}
[HttpPost] // This is the method I want to get executed.
public ActionResult GetSingleDateTab(HolidayRequestViewModel hrvm)
{
if (ModelState.IsValid)
{
hrvm.Update();
return View(new HolidayRequestViewModel(hrvm.EmployeeId));
}
return View("GetSingleDateTab", hrvm);
}
#endregion
You can't have nester forms in html.
I discover an unbelievable solution. Just insert a <script> tag above the <form> tag.
<script type="text/javascript">
// don't remove <script> tag
</script>
<form ....

MVC - fields in a partial View need a Unique Id. How do you do this?

In my view I render a partial view within a loop.
The problem I have is that for each new row, the Id of the fields remains the same.
I can I change this so that the Ids are unique and predictable?
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
BankHoliday
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
<% using (Html.BeginForm())
{%>
<h3>Bank Holiday Administration</h3>
<p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
<fieldset>
<legend>Enter the bank holidays here:</legend>
<table>
<tr>
<th>Bank Holiday</th>
<th>Date</th>
<th>Notes</th>
</tr>
<% foreach (var bankHolidayExtended in Model.BankHolidays)
{ %>
<% Html.RenderPartial("BankHolidaySummary", bankHolidayExtended); %>
<% } %>
<tr>
<td align="center" colspan="3" style="padding-top:20px;">
<input type="submit" value="Create" />
</td>
</tr>
</table>
</fieldset>
<% } %>
Partial View
" %>
<tr>
<td><%: Model.T.BankHolidayDescription%></td>
<td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate)%></td>
<td><%: Html.EditorFor(model => model.BH.BankHolidayComment)%></td>
</tr>
I would recommend you using editor templates instead of rendering partial views:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
BankHoliday
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
<% using (Html.BeginForm()) { %>
<h3>Bank Holiday Administration</h3>
<p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
<fieldset>
<legend>Enter the bank holidays here:</legend>
<table>
<tr>
<th>Bank Holiday</th>
<th>Date</th>
<th>Notes</th>
</tr>
<%: Html.EditorFor(x => x.BankHolidays) %>
<tr>
<td align="center" colspan="3" style="padding-top:20px;">
<input type="submit" value="Create" />
</td>
</tr>
</table>
</fieldset>
<% } %>
</asp:Content>
And then place your user control in ~/Views/Home/EditorTemplates/BankHolidayExtended.ascx (the name and location are important, replace Home with the name of your controller):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SHP.WebUI.Models.BankHolidayExtended>" %>
<tr>
<td><%: Model.T.BankHolidayDescription %></td>
<td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate) %></td>
<td><%: Html.EditorFor(model => model.BH.BankHolidayComment) %></td>
</tr>
This editor template will be automatically rendered for each element of the BankHolidays collection property on your view model. It will ensure that unique Ids are generated and the name of the input fields will be suitable for binding in the POST action. It also makes your code more readable.

submit View in ASP.NET MVC

How submit list View in ASP.NET MVC. List View dont have
input type="submit" value="Save"
and I dont know where to put it.
Problematic code is:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcZezanje.Models.student1>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Studenti
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Studenti</h2>
<table class="data-table">
<tr>
<th>
Br. Indexa
</th>
<th>
Prezime
</th>
<th>
Ime
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.id_stud) %>
</td>
<td>
<%= Html.Encode(item.prezime) %>
</td>
<td>
<%= Html.Encode(item.ime) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
</asp:Content>
What do you want to submit? This code displays list of students. There is no user input. If there is no user input, there is no need to place submit button. if you want to edit user data, you'll have to do something like:
<form method="post" action="/Students/Save">
<table class="data-table">
<tr>
<th>
Br. Indexa
</th>
<th>
Prezime
</th>
<th>
Ime
</th>
</tr>
<% int i = 0; foreach (var item in Model) { %>
<tr>
<td>
<%= Html.TextBox("students[" + i + "].id_stud", item.id_stud) %>
</td>
<td>
<%= Html.TextBox("students[" + i + "].prezime", item.prezime) %>
</td>
<td>
<%= Html.TextBox("students[" + i + "].ime", item.ime) %>
</td>
</tr>
<% i++;} %>
</table>
<input type="submit" />
</form>
Here you can read about binding to list:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
It has changed a little, you don't have to specify index anymore.

Resources