Paging in a scaffolding view MVC3 - asp.net-mvc-3

so using EF4 I create a scaffolding controller/view, so my question is how can I Add Paging to my view in easy/fast way??
the
controller generated
public ViewResult Index()
{
return View(db.Perifericos.ToList());
}
the
view: generated
#model IEnumerable<Paginacion.Models.Perifericos>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Nombre
</th>
<th>
Modelo
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Modelo)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.idPerifericos }) |
#Html.ActionLink("Delete", "Delete", new { id=item.idPerifericos })
</td>
</tr>
}
</table>

Maybe this can help you: http://www.asp.net/entity-framework/tutorials/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
You have to implement a paging in controller and in the view add code like the link that i have given to you.

I use this one http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
It's support also ajax and areas
Easy to implement.

a complete msdn article with sample code
Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application

Related

Under-hood explanation required for asp.net MVC tutorial

refer to asp.net MVC tutorial:
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view
regarding the autogenerated Views\Movies\SearchIndex.cshtml
Question 1:
<p>
#Html.ActionLink("Create New", "Create")
#using (Html.BeginForm())
{
<p>
Genre: #Html.DropDownList("movieGenre", "All")
Title: #Html.TextBox("SearchString", "Movies", FormMethod.Get)
<input type="submit" value="Filter" />
</p>
}
</p>
how does movieGenre refer to #ViewBag.movieGenre, which is obviously defined in Controllers/MoviesController.cs
Question 2:
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</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>
}
where is modelItem defined? VS2010 shows me modelItem is
IEnumerable <MvcMovie.Model.Movie>
Thanks.
Looks like you've already answered your second question, so I'll just answer your first:
The Html.DropDownList helper will bind itself to ViewData by default if no data is provided to it. ViewBag is simply a dynamic wrapper around the ViewData dictionary, so when you set ViewBag.movieGenre = new SelectList() you are effectively setting ViewData["movieGenre"] = new SelectList().
Now that you have this SelectList in your ViewData, the following will automatically bind this to the dropdown:
#Html.DropDownList("movieGenre")
This implicit binding isn't very well documented. See here for more information:
http://weblogs.asp.net/pjohnson/archive/2012/10/26/mvc-s-html-dropdownlist-and-quot-there-is-no-viewdata-item-of-type-ienumerable-lt-selectlistitem-gt-that-has-the-key.aspx

How to display Company in header in asp.net mvc 3

I have this view :
model IEnumerable<MvcApplication4.Models.Order>
#{
ViewBag.Title = "Index";
}
<h2>
display CompanyName
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Customer
</th>
<th>
EmployeeID
</th>
<th>
OrderDate
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Customer.CompanyName)
</td>
<td>
#Html.DisplayFor(modelItem => item.EmployeeID)
</td>
<td>
#Html.DisplayFor(modelItem => item.OrderDate)
</td>
</tr>
}
</table>
I want to display the CompanyName where I marked in bold. At the moment its being displayed in the foreach loop. However I want that one field which is same for this view to pull the CompanyName and display it in between my headings.
How about putting
#{
if (Model.Any()) {
Model.First().Customer.CompanyName
}
}
where your bold text is?

HTTP POST, GET in MVC?

In MVC3, i created one register form. I created one model,,controller and view. This is my code:
[HttpGet]
public ActionResult Insert()
{
Models.Employees objEmpl = new Models.Employees();
return View(objEmpl);
}
[HttpPost]
[AcceptVerbs("Post")]
public ActionResult Insert(Models.Employees objs)
{
var v = new Models.test().InsertDl(objs);
return View();
}
The above is my controller
#model MvcVideo.Models.Employees
#{
ViewBag.Title = "Insert";
Layout = "~/Views/Shared/VideoLayout.cshtml";
}
<h2>Insert</h2>
#using(Html.BeginForm("Insert","Home",FormMethod.Post ))
{
<table>
<tr>
<td>
Employee Name
</td>
<td>
#Html.TextBox("Ename",#Model.Enames)
</td>
</tr>
<tr>
<td>
Department Id
</td>
<td>
#Html.TextBox("Departmentid",#Model.DepartId )
</td>
</tr>
<tr>
<td>
Email Id
</td>
<td>
#Html.TextBox("Emailid",#Model.EmailIds)
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
#Html.TextBox("Address",#Model.Adress)
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;" >
<button title ="Ok" value="OK"></button>
</td>
</tr>
</table>
}
But the objs object at public actionresult Insert(models.Empoyees objs) action method parameter is showing null values. Imean Ename=Null, Department=0, Emailid=Null and Address=null
It isn't working because the names you've provided in your Html helpers don't match up with the property names on your model, so the default model binder can't resolve them when the values get posted back.
Using Html.TextBoxFor instead of Html.TextBox will provide you with strong typing against your model, and is the safer approach.
Replace this
#Html.TextBox("Ename",#Model.Enames)
With
#Html.TextBoxFor(model => model.Enames)
This will fix your issue.

How to deal with inherited entity in EF and MVC app?

I am developing MVC application and using razor syntax.
I have used model first method.
I have two entities, Customer and Lead. Lead is inherited from Customer.
When IsActive property is true then Customer treated as a Lead, otherwise it will be a customer.
Please check edmx file image.
When I genrated DB from model , I get the two tables Customer Table and Customer_Lead
table.
This is Customer table.
and This is Customer_Lead Table.
Now the problem is when I run index view of lead entity , Its give the error.
The model item passed into the dictionary is of type
PagedList.PagedList1[CRMEntities.Customer], but this dictionary
requires a model item of type
PagedList.IPagedList1[CRMEntities.Lead].
I am getting confused abt how to write a view for the entity which is inherited from other entity and how to access the data from these two tables while working with inherited entity.
Index View code is as below...
#model PagedList.IPagedList<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Status
</th>
<th>
IsQualified
</th>
<th>
Name
</th>
<th>
Address
</th>
<th>
OfficePhone1
</th>
<th>
Website
</th>
<th>
Email2
</th>
<th>
Email1
</th>
<th>
FaxNo
</th>
<th>
Remark
</th>
<th>
Rating
</th>
<th>
BusinessType
</th>
<th>
IsActive
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsQualified)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.OfficePhone1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
#Html.DisplayFor(modelItem => item.FaxNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.BusinessType)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsActive)
</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 have following code in index...
public ViewResult Index()
{
var LeadList = (from c in db.Customers
where (c.IsActive == true)
orderby (c.Id)
select c);
//What should I return ?
}
Your view looks fine, but you're trying to pass a list of Customers to it. Try getting a list of Lead customers from context.
public ActionResult Index()
{
...
var leadList = db.Customers.OfType<CRMEntities.Lead>().Where(c => c.IsActive).OrderBy(c => c.Id);
return View(leadList);
}

Post mvc3 table to controller action

I have a simple table
#using (Html.BeginForm("Save", "Subscription", FormMethod.Post))
{
<table>
<tr>
<th>
Name
</th>
<th>
Report
</th>
</tr>
#foreach (var item in Model.ReportSubscriptions)
{
<tr>
<td>
#Html.HiddenFor(item.Id)
#Html.TextBoxFor(item.Name)
</td>
<td>
#Html.TextBoxFor(item.Report)
</td>
</tr>
}
</table>
<button type="submit">Save</button>
}
The save action
public ViewResult Save(IEnumerable<ReportSubscription> list)
{
throw new NotImplementedException();
}
}
How can I get MVC to deserialize the post back to my model?
For collections you need to do a Editor template and use
#Html.EditorFor(m => m.Collection);

Resources