MVC 4 Paging error - asp.net-mvc

using this controller I am getting the error
the model item passed into the dictionary is of type 'System.Collections.Generic, but this dictionary requires a model item of type 'PagedList.IPagedList`
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
// Regular code here down
if (searchstring != null)
{
page = 1;
}
else
{
searchstring = currentFilter;
}
ViewBag.CurrentFilter = searchstring;
var teachers = from r in db.AdulLiteracyTeachers select r;
if (!string.IsNullOrEmpty(searchstring))
{
teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));
}
teachers = teachers.OrderBy(r => r.ALTName);
int pagesize = 10;
int pageNo = (page ?? 1);
return View(teachers.ToPagedList(pageNo, pagesize));
}
my view index.cshtml is :
#using PagedList;
#using PagedList.Mvc;
#model IPagedList<LiteracyPayroll.Models.AdulLiteracyTeachers>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index","AdultLiteracyTeachers",FormMethod.Get))
{
<p>
Find By Teacher Name: #Html.TextBox("searchstring",ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.First().District.DistName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTCLC)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTGEN)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTCNIC)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDOB)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTFName)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTAppNo)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDes)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTQuali)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTexp)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTDom)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTdoc)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBG)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTAddress)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTcontact)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBankno)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBankname)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTBcode)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTNTN)
</th>
<th>
#Html.DisplayNameFor(model => model.First().ALTPay)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Image)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.District.DistName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTCLC)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTGEN)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTCNIC)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDOB)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTFName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTAppNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDes)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTQuali)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTexp)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTDom)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTdoc)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBG)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTAddress)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTcontact)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBankno)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBankname)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTBcode)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTNTN)
</td>
<td>
#Html.DisplayFor(modelItem => item.ALTPay)
</td>
<td>
<img src="#Url.Content("~/Content/Uploads/" + "/" + item.Image + "")" alt="#item.Image" width="100px" height="100px" />
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ALTid }) |
#Html.ActionLink("Details", "Details", new { id=item.ALTid }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ALTid })
</td>
</tr>
}
</table>
<div>
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) Of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
</div>

The problem is here in below line when you check that "searchstring" and "currentFilter" is null. Currently it look like this -
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
// Regular code here down
blah... blah..
}
Change your code to say this instead should fix it:
// Initialize the pagesize and pageNo at the starting of the function
int pagesize = 10;
int pageNo = (page ?? 1);
if (searchstring == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToPagedList(pageNo, pagesize));
}
// Regular code here down
.......

Related

Is there a way I can add and bind checkboxes to my MVC Table

I am trying to add a checkbox so I can selects a row from my list and from there use that row to generate a letter.
I was wondering how I would be able to add a checkbox and bind it to the data I have tried a few ways using something similar to:
checkbox(m => m[i].Lesson)
However it would never accept the i in brackets and come up with an error. below is the layout of my index for the lessons model currently.
Here is the Lessons Index:
#model IEnumerable<FinalMusicApp.Models.Lesson>
#{
ViewBag.Title = "Index";
}
<h2>Lessons</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index", "Students", FormMethod.Get))
{
//Search options
<b>Search Options: </b> #Html.RadioButton("Option", "Instrument")<text>Instrument</text>#Html.RadioButton("Option", "Tutor")<text>Tutor</text>#Html.RadioButton("Option", "Term")<text>Term</text>#Html.TextBox("Search")
<input type="submit" name="submit" value="Search" />
}
<form method="post">
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.IsChecked)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonDate)
</th>
<th>
#Html.DisplayNameFor(model => model.LessonTime)
</th>
<th>
#Html.DisplayNameFor(model => model.Duration.TimeTaken)
</th>
<th>
#Html.DisplayNameFor(model => model.Instrument.InstrumentName)
</th>
<th>
#Html.DisplayNameFor(model => model.Letter.Paid)
</th>
<th>
#Html.DisplayNameFor(model => model.Student.FullName)
</th>
<th>
#Html.DisplayNameFor(model => model.Tutor.TutorName)
</th>
<th>
CRUD
</th>
<th>
Make a letter
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.CheckBoxFor(modelItem => item.IsChecked)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.LessonTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.Duration.TimeTaken)
</td>
<td>
#Html.DisplayFor(modelItem => item.Instrument.InstrumentName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Letter.Paid)
</td>
<td>
#Html.DisplayFor(modelItem => item.Student.FullName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Tutor.TutorName)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.LessonId }) |
#Html.ActionLink("Details", "Details", new { id = item.LessonId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.LessonId })
</td>
<td>
#Html.ActionLink("Generate Letter", "Create", "Letters")
</td>
</tr>
}
</table>
</form>
I am trying to add a checkbox so I can selects a row from my list
Do you want something like below?
In the view add a checkbox, for example I use the property CourseName in my model instead, you can use your property:
#Html.CheckBox("CourseName", new { value = item.CourseName })
#Html.DisplayFor(modelItem => item.CourseName)
Then in the controller, we can get CourseName value, and get other property value accord to the CourseName, but we need load the list again, we cannot send the full list from view to controller :
var courserow = model.FirstOrDefault(m => m.CourseName == CourseName);
result:

Object reference not set to an instance of an object. First login. solved when I rerun the application?

When I create a user and he's logged in for the first time I get this error when I try to display(View details) the requests that he created so far(0 request created). Once I rerun the application again with the same user the view works and I get no error. Here's the line that's generates the error:
#foreach (var item in Model)`
Here's the view that generates the error:
#model IEnumerable<Mutuelle.Domain.Entities.Demande>
#{
ViewBag.Title = "DemandesAdherent";
}
#{
Layout = "~/Views/Shared/_LayoutAdherent.cshtml";
}
<h2>DemandesAdherent</h2>
<p>
#Html.ActionLink("Creér nouvelle", "AddDemande")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.nomBeneficiaire)
</th>
<th>
#Html.DisplayNameFor(model => model.Nomrubrique)
</th>
<th>
#Html.DisplayNameFor(model => model.etat)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.nomBeneficiaire)
</td>
<td>
#Html.DisplayFor(modelItem => item.Nomrubrique)
</td>
<td>
#Html.DisplayFor(modelItem => item.etat)
</td>
<td>
#Html.ActionLink("Details", "DetailsDemande", new { id=item.DemandeId }) #*|
#Html.ActionLink("Delete", "DeleteDemande", new { id=item.DemandeId })*#
</td>
</tr>
}
</table>

how to make model into IEnumerable for the view [duplicate]

This question already has answers here:
The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
(7 answers)
Closed 5 years ago.
This is my ActionResult
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
return View(ee);
}
View is
#model IEnumerable<trydbconnectMvc.Models.Empoyee>
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<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>
</tr>
}
</table>
I am getting error
" The model item passed into the dictionary is of type 'trydbconnectMvc.Models.Empoyee', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[trydbconnectMvc.Models.Empoyee]'."
I am new to mvc. How to solve this?
It seems you're returning only one entity, not a IEnumerable. Just change it in your view:
#model trydbconnectMvc.Models.Empoyee
#{
ViewBag.Title = "mytry";
}
<h2>mytry</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.LastName)
</th>
<th>
#Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
#Html.DisplayNameFor(model => model.Address)
</th>
<th>
#Html.DisplayNameFor(model => model.Salary)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
<tr>
<td>
#Html.DisplayFor(modelItem => #Model.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.MiddleName)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Address)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Salary)
</td>
<td>
#Html.DisplayFor(modelItem => #Model.Email)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=#Model.Id }) |
#Html.ActionLink("Details", "Details", new { id=#Model.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=#Model.Id })
</td>
</tr>
}
</table>
or if you want, just change in your Controller:
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
var list = new List<Empoyee>(){ ee };
return View(list );
}
and keep your current view.
If you must use an IEnumerable, you can make a List<> and pass it in
public ActionResult mytry()
{
Empoyee ee = new Empoyee();
ee.Address = "address1";
ee.Email = "email1";
ee.FirstName = "fname";
ee.Id = 23;
ee.LastName = "lname1";
ee.MiddleName = "";
ee.Salary = 20000;
db.Empoyees.Add(ee);
db.SaveChanges();
List<Employee> employees = new List<Employee>();
employees.add(ee);
return View(employees);
}

How can I get value textbox inside partial view in asp.net mvc

How can I get value of #Html.TextBoxFor(modelItem => item.Sales) in partial view after pass to controller
My partial view :
#model IEnumerable<PG.Admin.Models.Items.ItemViewModel>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.First().ItemId)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Name)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Sales)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ItemId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.TextBoxFor(modelItem => item.Sales)
</td>
</tr>
}
</table>

redirect to a action that expects value in other controller

I have a controller StepOfIdea,and this controller has a action like this :
StepOfIdeaRepository objStepOfIdearepository=new StepOfIdeaRepository();
public ActionResult Index(int ideaId)
{
return View(objStepOfIdearepository.FindBy(i=>i.IdeaId==ideaId));
}
So i have another controller named idea and this controller has a view named index
#model IEnumerable<DomainClass.Idea>
#{
ViewBag.Title = "Index";
}
<h2>لیست</h2>
#if (User.IsInRole("User"))
{
<p>
#Html.ActionLink("ایده جدید", "Create", new {step = 1})
</p>
}
<table>
<tr>
#if (User.IsInRole("Admin"))
{
<th>
#Html.DisplayNameFor(model => model.User.Name)
</th>
}
<th>
#Html.DisplayNameFor(model => model.IdeaPersion)
</th>
<th>
#Html.DisplayNameFor(model => model.IdeaEnglish)
</th>
<th>
#Html.DisplayNameFor(model => model.IdeaResult)
</th>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th></th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
#if (User.IsInRole("Admin"))
{
<td>
#Html.DisplayFor(modelItem => item.User.Name) #Html.DisplayFor(modelItem => item.User.Name)
</td>
}
<td>
#Html.DisplayFor(modelItem => item.IdeaPersion)
</td>
<td>
#Html.DisplayFor(modelItem => item.IdeaEnglish)
</td>
<td>
#Html.DisplayFor(modelItem => item.IdeaResult)
</td>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.RenderAction("ویرایش","index", stepofidea, new { id=item.Id }) |
</td>
<td>
#Html.ActionLink("ویرایش", "Edit", new { id=item.Id }) |
#Html.ActionLink("نمایش", "Edit", new { id=item.Id }) |
#Html.ActionLink("حذف", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
In this line
#Html.RenderAction("ویرایش","index", ????, new { id=item.Id }) |
I want to redirect to index action of stepOfIdea controller and pass a value .But the above line doesn't work .
I think you confused some terms in translation to English, and what you are actually looking for is to create an Link that also passes a variable.
You can do this very simply, by:
#Html.ActionLink("ویرایش", "Index", "StepOfIdea", new { id = item.Id }, null)
This will create the HTML:
ویرایش

Resources