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)
{ %>
Related
<%# 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) %>
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
Greeting Everyone,
I'm having a little trouble with a partial view I'm using as an edit form. Aside from the hidden elements in the form, the model is null when passed to my EditContact post controller.
I should add, I realize there's a more seamless way to do what I'm doing using AJAX, but I think if I can solve the clunky way first it will help me understand the framcework a little better.
Here's my EditContact partial view:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<WebUI.DataAccess.CONTACT>" %>
<div id="contactPartial-<%= ViewData.Model.id %>">
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("EditContact", "Investigator"))
{ %>
<%= Html.HiddenFor(Model => Model.id) %>
<%= Html.HiddenFor(Model => Model.investId) %>
<table id="EditContact-<%= ViewData.Model.id %>" width="100%">
<tr>
<th colspan="4">
<b>New Contact</b>
</th>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.type)%></b><br />
<%= Html.EditorFor(Model => Model.type, null, "contactType-" + ViewData.Model.id)%><br />
<%= Html.ValidationMessageFor(Model => Model.type) %>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.salutation)%></b><br />
<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.firstName)%></b><br />
<%= Html.EditorFor(Model => Model.firstName, null, "contactFirstName-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.middleName)%></b><br />
<%= Html.EditorFor(Model => Model.middleName, null, "contactMiddleName-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.lastName)%></b><br />
<%= Html.EditorFor(Model => Model.lastName, null, "contactLastName-"+ViewData.Model.id)%><br />
<%= Html.ValidationMessageFor(Model => Model.lastName)%>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.title)%></b><br />
<%= Html.EditorFor(Model => Model.title, null, "contactTitle-"+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.phone)%></b><br />
<%= Html.EditorFor(Model => Model.phone, null, "contactPhone="+ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.altPhone)%></b><br />
<%= Html.EditorFor(Model => Model.altPhone, null, "contactAltPhone-" + ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.fax)%></b><br />
<%= Html.EditorFor(Model => Model.fax, null, "contactFax-" + ViewData.Model.id)%>
</td>
</tr>
<tr>
<td>
<b>
<%= Html.LabelFor(Model => Model.email)%></b><br />
<%= Html.EditorFor(Model => Model.email, null, "contactEmail-" + ViewData.Model.id)%>
</td>
<td>
<b>
<%= Html.LabelFor(Model => Model.comment)%></b><br />
<%= Html.EditorFor(Model => Model.comment, null, "contactComment-" + ViewData.Model.id)%>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" />
</td>
<td>
<button id="<%= ViewData.Model.id %>" class="contactEditHide">
Cancel</button>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<% } %>
</div>
When the Edit contact post controller is passed the model only id and investId have values; everything else is null. Here's how I'm rendering the partial view in my Details view:
<table width="100%">
<tr>
<th colspan="7">
<b>Contacts</b>
</th>
<th id="contactButton" class="button">
<button id="showEditContact-0" type="button" class="contactEditShow">New Contact</button>
</th>
</tr>
<tr>
<th>
Type
</th>
<th>
Name
</th>
<th colspan="2">
Title
</th>
<th>
Prim. & Alt. Phone
</th>
<th>
Fax
</th>
<th colspan="2">
Comment
</th>
</tr>
<% for (int i = 0; i < Model.CONTACTs.Count; i++)
{ %>
<tr id="contactRow-<%= Model.CONTACTs[i].id %>" class="contactRow">
<td>
<%= Html.Encode(Model.CONTACTs[i].type)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].salutation)%>
<%= Html.Encode(Model.CONTACTs[i].firstName)%>
<%= Html.Encode(Model.CONTACTs[i].middleName)%>
<%= Html.Encode(Model.CONTACTs[i].lastName)%>
<br />
<a href="mailto:<%= Html.AttributeEncode(Model.CONTACTs[i].email) %>">
<%= Html.Encode(Model.CONTACTs[i].email)%></a>
</td>
<td colspan="2">
<%= Html.Encode(Model.CONTACTs[i].title)%>
</td>
<td>
Prim:<%= Html.Encode(Model.CONTACTs[i].phone)%><br />
Alt:<%= Html.Encode(Model.CONTACTs[i].altPhone)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].fax)%>
</td>
<td>
<%= Html.Encode(Model.CONTACTs[i].comment)%>
</td>
<td>
<button id="<%= Model.CONTACTs[i].id %>" type="button" class="contactEditShow">Edit Contact</button>
</td>
</tr>
<tr>
<td colspan="8">
<% Html.RenderPartial("EditContact", Model.CONTACTs[i]); %>
</td>
</tr>
<% } %>
</table>
My details view is strongly tyed to an INVESTIGATOR model:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<WebUI.DataAccess.INVESTIGATOR>" %>
As you can see above, my partial view is strongly typed to a CONTACTS model.
I ran into a similar problem with Model binding when, in a view, I was trying to allow for the editing all layers of an address at one time and fixed it by indexing the model, similar to the Details view above. It seems like I need to do the same thing in my partial view to ensure Model binding, but I'm not sure how to achieve this (having syntax issues).
Of course, I could be off here an need a new solution in general!
Let me know if you need more detail! Thanks!
Why are you using
<%= Html.EditorFor(Model => Model.salutation, null, "contactsalutation-"+ViewData.Model.id)%>
If you use it simply like this
<%= Html.EditorFor(Model => Model.salutation) %>
It should be working.
In order to map your model, your control need to have the same id as there name, and the third parameter you're using changes your control id.
Use Html.EditorFor instead of Html.RenderPartial.
See Model binding with nested child models and PartialViews in ASP.NET MVC.
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.
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.