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/
Related
I need to pass a complex object to a .NET MVC controller from a View and i'm using Razor to do so. Here is my link:
<a href="#Url.Action("PlayVideo","Media",m)">
where "m" is an object of type MediaVM like this:
public class MediaVM
{
public int Id { get; set; }
public DateTime Date { get; set; }
public VCMediaType MediaType { get; set; }
public string RoomName { get; set; }
public string ThumbPath { get; set; }
public List<string> Sources { get; set; }
public string Description { get; set; }
public string CaseNumber { get; set; }
public string PatientFullName { get; set; }
}
The problem is with the "Sources" property; because it is a List of Strings, when i call the action at the "MediaController" it receives the MediaVM object correctly constructed except for the Sources property, which just contains a Collection with one String with the literal "System.Collections.Generic.List`1[System.String]".
The Constructor:
public ActionResult PlayVideo(MediaVM videoMedia)
{
return PartialView("Player", videoMedia);
}
How could i pass the complete list of strings? Is it possible?
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 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.
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));
}