I have a model like this
public class SearchVM
{
[DisplayName("Type")]
public string Type { get; set; }
[DisplayName("Beds")]
public string NumberOfBeds { get; set; }
[DisplayName("Baths")]
public string NumberOfBaths { get; set; }
[DisplayName("Prices From")]
public string PriceFrom { get; set; }
[DisplayName("Prices To")]
public string PriceTo { get; set; }
public IEnumerable<Rental> Rentals { get; set; }
public IEnumerable<Sale> Sales { get; set; }
public IEnumerable<Rental> FeatureRentals { get; set; }
public IEnumerable<Sale> FeatureSales { get; set; }
public Rental NewRentals { get; set; }
public Sale NewSales { get; set; }
}
In my controller when trying to use this model SearchVM i cannot use the ToPagedList method
public ActionResult Search(SearchVM searchvm, int page = 1)
{
var query = from c in context.Rentals
select c;
searchvm.Rentals = query;
return View("RentProperty", query.ToPagedList(page,9));
}
I noticed that searchvm.ToPagedList doesnt work. Can some one please help
You are either missing a library reference in your code or if you already have that, you need to add a relevant using statement. For example, you may need this at the top of your code:
using PagedList;
Without this line, the compiler doesn't know where to get the ToPagedList() extension method.
Related
I have the following models
Patient Class:
public class Patient
{
public int PatientID { get; set; }
public virtual Salutation salutation { get; set; }
public int SalutationID { get; set; }
public string Surname { get; set; }
public string Firstname { get; set; }
[Display(Name = "Date of Birth")]
[DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
public DateTime DOB { get; set; }
public string RegNo { get; set; }
public DateTime RegDate { get; set; }
public string Occupation { get; set; }
}
VatalSigns Class
public class VitalSign
{
public int VitalSignID { get; set; }
public string Sign { get; set; }
[Display(Name = "Lower Limit")]
public int? LowerHold { get; set; }
[Display(Name = "Upper Limit")]
public int? UpperHold { get; set; }
[Display(Name = "Unit Of Measurment")]
public string Units { get; set; }
}
PV class that stores VitalSigns for each patient
public class PVSign
{
public long PVSignId { get; set; }
[Display(Name = "Patient")]
public int PatientID { get; set; }
public VitalSign VitalSigns { get; set; }
//public IList<VitalSign> VitalSigns { get; set; }
public Patient patient { get; set; }
}
Now the problem I have is that I have not been able to get display on one from to enter the details. I want to select the Patient and the different Vitals signs will appear and I will save the ones I need to the PVSign table.
I have tired all sorts of samples from the web. You can see from the code below, this is the index stub:
public ActionResult Index()
{
var pVSigns = db.PVSigns.Include(p => p.patient).Include(p => p.PVSignId).Include(p => p.VitalSigns);
//var pVSigns = from o in db.Patients join o2 in db.PVSigns
//List<object> myModel = new List<object>();
//myModel.Add(db.Patients.ToList());
//myModel.Add(db.VitalSigns.ToList());
//return View(myModel);
return View(pVSigns.ToList());
}
How to I solve this issue. I am new to MVC, if it was Webforms, I would have been through with this project.
Thank you.
There is no single answer(solution) to this(your) problem. It depends on how you want to design/build/achieve your solution.
The simple, brute solution : Just have a view model as a wraper that has as his properties the classes(models) you made and work around that,
public class FullPatientDetailsViewModel
{
public Patient { get; set;}
public List<PVSign> PatientPvSigns { get; set;} // Patien PV Signs
}
Or use just a PatientViewModel and load his PVSigns async with Ajax.
There is no simple best solution, it all depends about what do you want to achieve as a bigger picture.
I've answered a similar question here. As Alexandru mentioned, the easiest and most straightforward way would be to wrap your models inside a ViewModel. You can read about other ways to implement this here.
Does anyone know of a standard library (or model binder) in C# to parse JQuery-style serialized filters (and sorting), like the one passed from the Kendo DataSource?
So to parse a query string like this to strongly typed array(s) with filter and sort objects:
sort[0][field]=status.name&sort[0][dir]=desc&filter[logic]=and&filter[filters][0][field]=status.name&filter[filters][0][operator]=eq&filter[filters][0][value]=Sold
I've tried searching but I mostly seem to run into incomplete implementations or JavaScript samples unfortunately.
UPDATE: I didn't realise this format is already standard supported until #Lali pointed out this is standard body form serialization.
I solved it now by creating some classes and just using this as parameter for the web API action, and it just magically works:
public async Task<IHttpActionResult> Get([FromUri] ListSelectionOptionsWithFilter options)
{
//query something
}
public class ListSelectionOptionsWithFilter
{
public int? Skip { get; set; }
public int? Take { get; set; }
public List<ListSortOption> Sort { get; set; }
public ListFilterOptions Filter { get; set; }
}
public class ListFilterOptions
{
public string Logic { get; set; }
public List<ListFilterOption> Filters { get; set; }
}
public class ListFilterOption
{
public string Field { get; set; }
public string Operator { get; set; }
public string Value { get; set; }
}
public class ListSortOption
{
public string Field { get; set; }
public string Dir { get; set; }
}
I didn't realise this format is already standard supported until #Lali pointed out this is standard body form serialisation.
I solved it now by creating some classes and just using this as parameter for the web API action, and it just magically works:
public async Task<IHttpActionResult> Get([FromUri] ListSelectionOptionsWithFilter options)
{
//query something
}
public class ListSelectionOptionsWithFilter
{
public int? Skip { get; set; }
public int? Take { get; set; }
public List<ListSortOption> Sort { get; set; }
public ListFilterOptions Filter { get; set; }
}
public class ListFilterOptions
{
public string Logic { get; set; }
public List<ListFilterOption> Filters { get; set; }
}
public class ListFilterOption
{
public string Field { get; set; }
public string Operator { get; set; }
public string Value { get; set; }
}
public class ListSortOption
{
public string Field { get; set; }
public string Dir { get; set; }
}
I have to pass array of filters like this:
Script Code:
return { CustomFilter: options.filter.filters };
++++++++++++++++++++++++++++++++++++++++++++++++
From Firebug:
CustomFilter[0][field] ItemName
CustomFilter[0][operator] startswith
CustomFilter[0][value] testing Value
CustomFilter[1][field] BrandName
CustomFilter[1][operator] startswith
CustomFilter[1][value] testing Value 1
Posted values are:
But i am unable to received these on Controller side.
i tried like this:
public ActionResult ReadOperation( string[][] CustomFilter)
Also like this:
public ActionResult ReadOperation( Filter[] CustomFilter)
public class Filter
{
public string field { get; set; }
public string #operator { get; set; }
public string value { get; set; }
}
But didn't work. Please Suggest.
Thank you.
Solution Found with Json deserialization
Script code changed to:
return { CustomFilter: JSON.stringify(CustomFilter) };
Controller Code changed to:
using Newtonsoft.Json;
public ActionResult ReadOperation(MyViewModel model)
{
var filters = JsonConvert.DeserializeObject(model.CustomFilter, typeof(CustomFilter[]));
}
public class MyViewModel
{
public string Filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
public class CustomFilter
{
public string field { get; set; }
public string #operator { get; set; }
public string value { get; set; }
}
Result View in controller:
It looks like an error with model structure.
public class MyViewModel
{
public Filter[] CustomFilter { get; set; }
public string Filter { get; set; }
public string Group { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
public int Sort { get; set; }
}
Try to use this type for model binding.
public ActionResult ReadOperation(MyViewModel model)
If you are using ASP.NET MVC 4, and cannot change the parameter names, maybe you can define a custom model in this way:
public class MyViewModel
{
public Dictionary<string,string>[] CustomerFilter { get; set; }
public string filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
Then, at the controller:
public ActionResult ReadOperation(MyViewModel model){ ... }
It seems that the notation used in the parameters generated by your grid is for dictionaries. Haven't tried a collection of Dictionaries, though.
In the post, try to send the data as this:
CustomFilter[0].Field ItemName
CustomFilter[0].Operator startswith
CustomFilter[0].Value testing Value
CustomFilter[1].Field BrandName
CustomFilter[1].Operator startswith
CustomFilter[1].Value testing Value 1
And at the controller:
public ActionResult ReadOperation(Filter[] CustomFilter)
Having a Filter class defined as:
public class Filter
{
public string Field { set; get; }
public string Operator { set; get; }
public string Value { set; get; }
}
(Be careful with the capital letters).
Or if you want to use the model approach, as Ufuk suggests, and having the same Filter class:
Model:
public class MyViewModel
{
public Filter[] CustomerFilter { get; set; }
public string Filter { get; set; }
public string Group { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
public int Sort { get; set; }
}
Parameters in POST:
CustomFilter[0].Field ItemName
CustomFilter[0].Operator startswith
CustomFilter[0].Value testing Value
CustomFilter[1].Field BrandName
CustomFilter[1].Operator startswith
CustomFilter[1].Value testing Value 1
Filter ItemName~startswith~'12'~and~BrandName~startswith~'123'
Group
Page 1
PageSize 15
Sort
Controller
public ActionResult ReadOperation(MyViewModel model)
See this link: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
Want to use two models in one view. I have two controllers one for current user
public class ProfileModel
{
public int ID { get; set; }
public decimal Balance { get; set; }
public decimal RankNumber { get; set; }
public decimal RankID { get; set; }
public string PorfileImgUrl { get; set; }
public string Username { get; set; }
}
and second for firends
public class FriendsModel
{
public int ID { get; set; }
public string Name { get; set; }
public string ProfilePictureUrl { get; set; }
public string RankName { get; set; }
public decimal RankNumber { get; set; }
}
Profile model contains always one item and Friend model contains list
I have made new model which contains both models :
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public FriendsModel FriendModel { get; set; }
}
I tried to fill FullProfile model like this
List<FriendsModel> fmList = GetFriendsData(_UserID);
FullProfileModel fullModel = new FullProfileModel();
fullModel.ProfileModel = pm;
fullModel.FriendModel = fmList.ToList();
but visual studio gives an error on .ToList()
error:
Cannot implicitly convert type 'System.Collections.Generic.List<NGGmvc.Models.FriendsModel>' to 'NGGmvc.Models.FriendsModel'
Please advice me something how i can show two models in single view.
p.s. im using mvc3 razor view engine
Thanks
You are trying to set a property of type FriendsModel with a value of List.
public FriendsModel FriendModel { get; set; }
Change to:
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public IList<FriendsModel> FriendModel { get; set; }
}
Correct your ViewModel
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public IList<FriendsModel> FriendModels { get; set; }
}
I think you need collection
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public List<FriendsModel> FriendModels { get; set; }
}
My Service layer returns a IEnumerable<ResortsView> to my Controller and I have to provide an option to SORT the results on UI by following criteria: Price (Excluding ZERO Price Items), SupplierName & Rating. Here is the code snippet of my objects/classes:
[Serializable]
public class ResortsView : SupplierView
{
public IList<ProductView> ResortProducts { get; set; }
public string CheckInTime { get; set; }
public string CheckOutTime { get; set; }
public virtual IList<ImageView> Images { get; set; }
}
[Serializable]
public class SupplierView
{
public string SupplierID { get; set; }
public string SupplierName { get; set; }
public string Description { get; set; }
public string Rating { get; set; }
public string URL { get; set; }
}
[Serializable]
public class ProductView
{
public string Code { get; set; }
public string Description { get; set; }
public decimal? Price { get; set; }
public ErrorView PricingError { get; set; }
}
What is the best way to handle sorting of results inside IEnumerable<ResortsView>?
Use the OrderBy method of the LINQ stack:
IEnumerable<ResortsView> source = GetResorts();
var model = source.OrderBy(rv => rv.CheckInTime);
To order by the lowest, non-zero price in ResortProducts:
var model = source.OrderBy(rv => rv.ResortProducts.Where(p => p.Price > 0).Min(p => p.Price));
Most likely if you'll want to use the Dynamic LINQ library, here are some instructions and a download link.
You'll need to have your action accept some sorting parameters:
public ActionResult MyAction(string sortField)
{
return View(myService.MyData().OrderBy(sortField));
}