MVC complex data query - asp.net-mvc-3

I have a query as below :
`Table
London flat
London flat
London House
Paris flat
Paris flat
Paris House
Paris House`
I am looking to turn this to below object in linq. Anyone please help me.
public class BrowseModel
{
public string TownName { get; set; }
public int FlatCount { get; set; }
public int HouseCount { get; set; }
}
The result need to be like:
London 2Flat 1 house
Paris 2Flat 2 houses

Hi if i haven't understood your question wrong you can try it like this
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
//Ignore above code
List<Table> list = new List<Table>();
//Suppose this what you db returns
list.Add(new Table() { TownName = "London", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "London", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "London", HouseType = HouseType.House });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.Flat });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.House });
list.Add(new Table() { TownName = "Paris", HouseType = HouseType.House });
var result = list.GroupBy(o => o.TownName).Select(s => new BrowseModel() { TownName = s.First().TownName, FlatCount = s.Where(f => f.HouseType == HouseType.Flat).Count(), HouseCount = s.Where(h => h.HouseType == HouseType.House).Count() }).ToList();
}
}
public class BrowseModel
{
public string TownName { get; set; }
public int FlatCount { get; set; }
public int HouseCount { get; set; }
}
public class Table
{
public string TownName { get; set; }
public HouseType HouseType { get; set; }
}
public enum HouseType
{
House=0,
Flat=1
}
I hope this will give you idea.

Related

Setting the default value of multiple dropdown lists on page load

I have a model,
public class Customer
{
public string Name { get; set;}
public string CountryCode { get; set;}
}
In the controller
var model = new List<Customer>
{
new Customer { Name = "foo", CountryCode = "US"},
new Customer { Name = "bar", CountryCode = "UK",
};
return PartialView("_Edit", model);
An extension method for displaying all countries:-
public class CountryList
{
public static IEnumerable<SelectListItem> CountrySelectList
{
get
{
var list = new List<SelectListItem>()
{
new SelectListItem { Value = "US", Text="US" },
new SelectListItem { Value = "UK", Text="UK" },
};
return list;
}
}
}
In the PartialView
#model List<Customer>
#Html.DropDownListFor(model => model[i].CountryCode, CountryList.CountrySelectList, "Select Country Type")
But the drop down doesn't select each customer's country code? Any thoughts?
PS: It is using model[i] => which is of type Customer, for simplicity i had removed the forloop before rendering the html tags.
#using(Html.BeginForm())
{
for(int i = 0; i < Model.Count(); i++)
{
#Html.TextBoxFor(model => model[i].Name)
#Html.DropDownListFor..........
}
}
Because your CoutryList helper does returns a list of SelectListItems that all have Selected property set to False (which is default).
I would rewrite your helper method as follows:
public static IEnumerable<SelectListItem> CountrySelectList(string selectedCountryCode)
{
get
{
var list = new List<SelectListItem>()
{
new SelectListItem { Value = "US", Text="US" },
new SelectListItem { Value = "UK", Text="UK" },
};
var selectedListItem = list.FirstOrDefault(t=>t.Value== selectedCountryCode);
if(selectedListItem!=null)
selectedListItem.Selected=true;
return list;
}
}
In view:
#Html.DropDownListFor(model => model[i].Customer, CountryList.CountrySelectList(model[i].Customer.CountryCode), "Select Country Type")

How to load and search data using edmx

I am trying to load the data into a dropdownlist in a nested master page but the Model in View is always null. How can I do this?
In model
public class AllInOneModel
{
public string City { get; set; }
public IEnumerable<City> cities { get; set; }
}
In controller
public ActionResult Add()
{
return View(new AllInOneModel());
}
In View
#model IBilik.Models.AllInOneModel
#Html.DropDownListFor(model => model.City,(SelectList)Model.cities)
Personally I would skip the "AllInOneModel" and stick to sending the single city to the view.
The controller action could look somewhat like this.
public ActionResult Index()
{
//TODO: get current cities from database
List<City> cities = new List<City>
{
new City { CityID = 1, CityName = "City1" },
new City { CityID = 2, CityName = "City2" },
new City { CityID = 3, CityName = "City3" }
};
// Create the list of selectlistitems to populate the dropdown
List<SelectListItem> listItems = new List<SelectListItem>();
foreach(var c in cities)
{
listItems.Add(new SelectListItem { Value = c.CityID.ToString(), Text = c.CityName });
}
//Throw them into viewbag
ViewBag.ListItems = listItems;
//TODO get some city (perhaps from db) to send to view
var city = new City { CityID = 1, CityName = "City1" };
return View(city);
}
And then you would render the view in this way.
#model IBilik.Models.City
#Html.DropDownListFor(model => model.CityID, ViewBag.ListItems as List<SelectListItem>)

MVC 3, code-first, and sample data

I'm using code-first, and am trying to generate some sample data, but am stuck.
var ts = new List<VehicleType>
{ new VehicleType { TypeName = "Car" },
new VehicleType { TypeName = "Truck" },
new VehicleType { TypeName = "Van" },
new VehicleType { TypeName = "SUV" },
new VehicleType { TypeName = "RV" }
};
var mr = new List<Manufacturer>{
new Manufacturer { ManufacturerName = "Ford" },
new Manufacturer { ManufacturerName = "Dodge" },
new Manufacturer { ManufacturerName = "Hyundai" },
new Manufacturer { ManufacturerName = "Mazda" },
new Manufacturer { ManufacturerName = "Honda" },
new Manufacturer { ManufacturerName = "Lexus" },
new Manufacturer { ManufacturerName = "Cadillac" },
new Manufacturer { ManufacturerName = "Jayco" }
};
var vm = new List<VehicleModel>{
new VehicleModel { ModelName = "Focus" },
new VehicleModel { ModelName = "Elantra" },
new VehicleModel { ModelName = "3" },
new VehicleModel { ModelName = "4X4" },
new VehicleModel { ModelName = "CRX" },
new VehicleModel { ModelName = "Element" },
new VehicleModel { ModelName = "SE 541" },
new VehicleModel { ModelName = "LE 4X" }
};
new List<Inventory>
{
new Inventory {VehicleModel = vm.Single(g => g.ModelName == "Focus"), Manufacturer = mr.Single(g => g.ManufacturerName == "Ford"), VehicleType = ts.Single(g => g.TypeName == "Car"), Price = 10999, Mileage = 241092, Year = 2001, Description = "Includes all G37x AWD Sport standard equipment FORD", Colour = "Tan", CarImageUrl = "/Content/Images/car1.jpg", DateReceived = "01/06/2011"},
}.ForEach(a => context.Inventorys.Add(a));
This works fine. My Inventory table in populated with the data from the other tables (VehicleType, Manufacturer, and VehicleModel). Here are their models:
public class Manufacturer
{
[Key]
public int ManufacturerId { get; set; }
public string ManufacturerName { get; set; }
}
public class VehicleModel
{
[Key]
public int ModelID { get; set; }
public string ModelName { get; set; }
}
public class VehicleType
{
[Key]
public int TypeId { get; set; }
public string TypeName { get; set; }
}
Now lets say the Manufacturer model had another attribute besides ManufacturerName, let's say it also has ManufacturerLocation. When I try to add that attribute into the Inventory list:
new List<Inventory>
{
new Inventory {VehicleModel = vm.Single(g => g.ModelName == "Focus"), Manufacturer = mr.Single(g => g.SomeAttributeFromManufacturerTable)
All attributes from the Manufacturer table will appear, but after I use one of those attributes, there doesn't seem to be an opportunity to enter in the rest of the attributes. Is this because it expects me to enter in the ManufacturerId attribute, and it will automatically associate the rest on the attributes through its primary key? I hope I'm making sense, I find this hard to explain.

SelectList in MVC3

I have a MVC3 page that has to dropdownList contains Sex(male,female) and Role(admin,Operator), in RegisterViewModel I have done like this :
public List<SelectListItem> SelectedItemForSex { get; set; }
public List<SelectListItem> SelectedItemForRole { get; set; }
public string SearchText { get; set; }
public string SelectedValue { get; set; }
//public string SelectedValueForRole { get; set; }
public static RegisterViewModel Get()
{
var model = new RegisterViewModel { SelectedItemForSex = new List<SelectListItem>() };
model.SelectedItemForSex.Add(new SelectListItem() { Text = "Male", Value = "1" });
model.SelectedItemForSex.Add(new SelectListItem() { Text = "Femle", Value = "2" });
model.SelectedItemForRole.Add(new SelectListItem() {Text = "Administrator", Value = "1"});
model.SelectedItemForRole.Add(new SelectListItem() {Text = "Operator", Value = "2"});
return model;
}
and in get action of Register I have this code :
public ActionResult Create()
{
var model = RegisterViewModel.Get();
return View(model);
}
and also in razor :
<div>
#Html.LabelFor(register => register.Sex)
#Html.DropDownListFor(register => register.SelectedValue, new SelectList(Model.SelectedItemForSex, "Value", "Text"))
</div>
<div>
#Html.LabelFor(register => register.Role)
#Html.DropDownListFor(register => register.SelectedValue, new SelectList(Model.SelectedItemForRole, "Value", "Text"))
</div>
<div>
I know I have Not initialize selectedListItem for Administrator and operator , I wanna to send all of this 4 value via Get() method, How can I do this ??

Multiple ViewModels in View

I'm looking to display two Telerik Kendo grids with different information(models) in one view and I'm not sure how to do it.
Model:
using System.Collections.Generic;
namespace KendoUIMvcApplication1.Models
{
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Truck
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Vehicles
{
public IEnumerable<Car> CarCol { get; set; }
public IEnumerable<Truck> TruckCol { get; set; }
}
}
Controller (Creating some test objects):
using System.Collections.Generic;
using System.Web.Mvc;
using KendoUIMvcApplication1.Models;
namespace KendoUIMvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var carList = new List<Car>();
var truckList = new List<Truck>();
var vehCol = new Vehicles();
var car1 = new Car
{
Id = 1,
Name = "Passat"
};
var car2 = new Car
{
Id = 2,
Name = "Passat"
};
carList.Add(car1);
carList.Add(car2);
var truck1 = new Truck
{
Id = 1,
Name = "S10"
};
var truck2 = new Truck
{
Id = 1,
Name = "Blazer"
};
truckList.Add(truck1);
truckList.Add(truck2);
vehCol.CarCol = carList;
vehCol.TruckCol = truckList;
return View(vehCol);
}
}
}
View (Here I'm trying to display the two grids. The first one for cars and the second one for trucks):
#using KendoUIMvcApplication1.Models
#model IEnumerable<Vehicles>
#*// Car Grid*#
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns => {
columns.Bound(c => c.Id);
columns.Bound(c => c.Name);
})
)
<div></div>
#*// Truck Grid*#
#(Html.Kendo().Grid(Model))
.Name("Grid")
.Columns(columns => {
columns.Bound(t => t.Id);
columns.Bound(t => t.Name);
})
)
Your model should be of type Vehicles:
#model Vehicles
Then you can access the two collections:
#Html.Kendo().Grid(Model.CarCol)
...
and
#Html.Kendo().Grid(Model.TruckCol)
...

Resources