mvc-Passing query data from controller to view - asp.net-mvc

i want to show my data from DB with query.
now i see the data but it show all table (dog) and i dont
successful combain query.
i want add query for display the dog_num from the smallest to grow
select * from dog order By dog_num desc
my code:
view
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.dog_num)
</th>
<th>
#Html.DisplayNameFor(model => model.dog_name)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.dog_num)
</td>
<td>
#Html.DisplayFor(modelItem => item.dog_name)
</td>
</tr>
controller
private model db = new model();
public ActionResult Index()
{
return View(db.dog.ToList());
}

alex, this is very simple
You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending
like:
.OrderByDescending(x => x.dog_num);
Example:
your Tsql:
select * from dog order By dog_num desc
Equivalent linq:
db.dog.ToList().OrderByDescending(x => x.dog_num);
Final controller code:
private model db = new model();
public ActionResult Index()
{
return View(db.dog.ToList().OrderByDescending(x => x.dog_num));
}
Hope this is what you want ,kindly let me know your thoughts or feedbacks
Thanks
karthik

Related

Incorrent model passed to Partial View

This might be a duplicate question but I can't for the life of me know what the solution is. I'm trying to load a partial view with a table based on the ID of a clicked link.
I'm getting this error:
System.InvalidOperationException: 'The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[MasigasigTrackingSystem.Models.Invoice]', but this ViewDataDictionary instance requires a model item of type 'MasigasigTrackingSystem.Pages.Masigasig.MainPage.TripTicket.TripTicketListModel'.'
I can't seem to figure out how should I passed the model to the partial view.
Here is what my OnGet looks like:
public IList<Invoice> Invoice { get; set; }
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice
select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", Invoice);
}
Here is my partial view:
#model TripTicketListModel
<h1>Hello, test</h1>
<table id="TripEntryList" class="table table-bordered table-hover">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Invoice[0].InvoiceNumber)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.Invoice)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
</tr>
}
</tbody>
You Partial View Required TripTicketListModel and you are passing the model Invoice. It means you are passing wrong model.
From your View code i can say your Invoice is used inside TripTicketListModel, so try below solution.
public PartialViewResult OnGetDetails(int TripTicketID)
{
var invoices = from m in _context.Invoice select m;
invoices = invoices.Where(x => x.TripTicketID == TripTicketID);
TripTicketListModel model = new TripTicketListModel();
model.Invoice = invoices.ToList();
return Partial("_TripTicketListDetails", model);
}

MVC ActionResult isn't grabbing Where expression correctly

I'm working on an animal shelter application. I have an AnimalInitializer.cs in my DAL and I have it broken down into dogs and cats by species. In my controller, I want to see the list of dogs for one ActionResult and a list of cats for another ActionResult. They are all returning as dogs when I run the ShowDogs view and nothing is returned on the ShowCats view.
Pet model:
public enum SpeciesType { Dog, Cat }
public class Pet
{
public SpeciesType SpeciesType { get; set; }
}
AnimalInitializer seed:
var dogs = new List<Dog>
{
new Dog { SpeciesType = SpeciesType.Dog, Breed = "German Shepherd" }
}
var Cats = new List<Cat>
{
new Cat { SpeciesType = SpeciesType.Cat, Breed = "Domestic Long Haired" },
}
PetController:
public ActionResult ShowDogs()
{
var dogs = db.Pets.Where(d => d.SpeciesType == SpeciesType.Dog);
return View(dogs.ToList());
}
public ActionResult ShowCats()
{
var cats = db.Pets.Where(c => c.SpeciesType == SpeciesType.Cat);
return View(cats.ToList());
}
ShowDogs View:
#model IEnumerable<AnimalShelter1.Models.Pet>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Breed)
</th>
<th>
#Html.DisplayNameFor(model => model.SpeciesType)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Breed)
</td>
<td>
#Html.DisplayFor(modelItem => item.SpeciesType)
</td>
</tr>
}
</table>
ShowCats View:
#model IEnumerable<AnimalShelter1.Models.Pet>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.SpeciesType)
</th>
<th>
#Html.DisplayNameFor(model => model.Breed)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SpeciesType)
</td>
<td>
#Html.DisplayFor(modelItem => item.Breed)
</td>
</tr>
}
</table>
I am not sure whether the below solution works. But please try this:
public ActionResult ShowDogs()
{
var dogs = db.Pets.Where(d => d.SpeciesType == SpeciesType.Dog).ToList();
return View(dogs);
}
public ActionResult ShowCats()
{
var cats = db.Pets.Where(c => c.SpeciesType == SpeciesType.Cat).ToList();
return View(cats);
}
I just ended up editing the ones that were supposed to be cats and saving them and that seemed to have worked. Every time I launch the application, the animals are in their correct places, and the breakpoints for ShowDogs and ShowCats are both getting hit and populated with the correct animals. Weird, I don't know what went wrong with it...

Bind viewmodel to partial view

I have a model, which can represent 3 categories. I want in my view, make 3 different tables for each category with relevant fields. I think for this I need to use partial view with viewmodel for each category.
So my main model is "Ad", which have 3 sub viewmodels (Realty, Auto and Service).
Here the example how I implement Realty action on my home controller:
public ActionResult Realty()
{
var ads = db.Ads.Include(a => a.Realty);
var vm = new List<RealtyViewModel>();
foreach (var ad in ads)
{
vm.Add(new RealtyViewModel
{
Title = ad.Title,
Descirpiton = ad.Descirpiton,
Type = ad.Realty.Type,
NumberOfRooms = ad.Realty.NumberOfRooms
});
}
return PartialView(vm);
}
Then my partial view, looks like this:
#model IEnumerable<OGAS.Areas.Category.ViewModels.RealtyViewModel>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Title)
</th>
<th>
#Html.DisplayNameFor(model => model.Type)
</th>
<th>
#Html.DisplayNameFor(model => model.Descirpiton)
</th>
<th>
#Html.DisplayNameFor(model => model.NumberOfRooms)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.Type)
</td>
<td>
#Html.DisplayFor(modelItem => item.Descirpiton)
</td>
<td>
#Html.DisplayFor(modelItem => item.NumberOfRooms)
</td>
</tr>
}
</table>
Then in my Index page (without using any models), I call partial view like this:
#{Html.RenderPartial("Realty");}
But then I'm getting following error:
An exception of type 'System.NullReferenceException' occurred in App_Web_gdyh352c.dll but was not handled in user code
Could you please advise if this approach is good (calling 3 vms), if yes how to implement this?
Thanks.
Try to replace #{Html.RenderPartial("Realty");} and use #Html.Action("Realty") in this case, as you need to call back to the controller action, in order to create the model for the partial view.
See MVC Html.Partial or Html.Action for more information.
Use this, for .net core and mvc. #Html.Action has been removed from .net core
#await Html.PartialAsync("_YourPartialViewName", YourModel)

MVC Object(x) does not contain a definition for blah

Getting an error on the view, at the displaynamefor softwareid line, saying the model SoftwareDTO does not contain a definition for softwareid. I can see it right there in the model.
Model:
public class SoftwareDTO
{
public int SoftwareId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Controller:
public ActionResult Index()
{
List<SoftwareDTO> softwareList = new List<SoftwareDTO>();
var data = _db.Software.ToList();
foreach (var sw in data)
{
SoftwareDTO software = new SoftwareDTO()
{
SoftwareId = sw.SoftwareId,
Name = sw.Name,
Description = sw.Description
};
softwareList.Add(software);
};
return View(softwareList);
}
View:
#model List<Request.Models.SoftwareDTO>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.SoftwareId)
</th>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.SoftwareId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
its because model its a list not an object SoftwareDTO in your razor view
I think you are missing the foreach
SoftwareId is a property of SoftwareDTO class. Your view is strongly typed to a collection of SoftwareDTO objects. So you need to loop through the model(The collection of SoftwareDTO) and access the SoftwareId of each item.
#model List<Request.Models.SoftwareDTO>
<table class="table">
#foreach(var item in Model)
{
<tr>
<td>
#Html.DisplayNameFor(x=> item.SoftwareId)
</td>
</tr>
}
</table>
EDIT : As per the edit in the question, and the comments provided.
Looks like you want to print the display name of the propertes in your table headers. If you do not wish to change the data you are passing from your action method, you can try this
#if (Model.Any())
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(x => Model[0].SoftwareId)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>
}
This is using the first item in the collection and it's properties to use with DisplayNameFor method. Since i have a if condition to check for at least one item before rendering the table, It will not even render the table if your Model has 0 items.
If you want to show the empty table with headers, you have 2 options.
Write HTML markup for the table header
<table class="table">
<tr>
<th>
<label>Software Id</label>
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>
Or if you still want to use the DisplayNameFor helper method to render the table header labels,
Create a new viewmodel
public class TableListVm
{
public List<SoftwareDTO> Items {set;get;}
public SoftwareDto ItemMeta {set;get;}
public TableListVm()
{
ItemMeta= new SoftwareDto();
}
}
And in your GET action, Send this object to your view
public ActionResult Index()
{
var data = _db.Software.ToList().Select(sw=> new SoftwareDTO {
SoftwareId = sw.SoftwareId,
Name = sw.Name,
Description = sw.Description
}).ToList();
var vm= new TableListVm { Items = data };
return View(vm);
}
And in your view which is strongly typed to this new view model.
#model TableListVm
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(x => Model.ItemMeta.SoftwareId)
</th>
</tr>
#foreach (var item in Model.Items)
{
<tr>
<td>
#Html.DisplayFor(x => item.SoftwareId)
</td>
</tr>
}
</table>

How to display list items in view passed from the controller ?

I am developing MVC app.
I want to create the list in the controller and pass it to the view.
I have written the method in the controller but dont know how to call it form view and display the value which it return.
Method in controller.
public List<Invoice> GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return (Invoices);
}
View Code
<div class="row-fluid">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Advice No
</th>
<th>
Invoices
</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
I wan to call the controller method GetInvoiceList here and
want to display list items here...
<td>
</tr>
</tbody>
Add a PartialView to your project that has it's model set to List<Invoice>
then modify your code:
public PartialViewResult GetInvoiceList(int Pid)
{
List<Invoice> Invoices = new List<Invoice>();
var InvoiceList = (from i in db.Invoices
where i.PaymentAdviceId == Pid
select i);
Invoices = InvoiceList.ToList();
return PartialView("partialViewName", Invoices);
}
in your other view:
<tr>
<td>
#Html.DisplayFor(modelItem => item.AdviceNo)
</td>
<td> #Html.Action("GetInvoiceList", new {Pid = item.id})</td>
</tr>

Resources