0

I wondering what is the best way to accomplish this task: In my index method I'm either searching or filtering or just taking data from my db. Then I send n items to view. If there more than n items I need some paging to be done. As I understand I can query my db again filter or search and take next n elements or I can somehow persist all my data(filtered or data received after search query) and just take n next elements from it. As I understand session is not the best way to accomplish it, so instead of IEnumerable of this class

public class CatalogModel
{
    public string Price;
    public string deviceName;
    public string imgUrl;
} 

I've decided to use this class

public class CatalogView
{
    public IEnumerable<CatalogModel> Catalog;
    public IEnumerable<Device> Devices;

    public CatalogView(IEnumerable<CatalogModel> catalog = null, IEnumerable<Device> devices = null)
    {
        Catalog = catalog;
        Devices = devices;
    }
}

I wand to keep all my data in

 public IEnumerable<Device> Devices 

this field and each time send it to controller, but I don't know how can I access it from javascript so I can post it back with ajax.

Is it possible to do it such way and will it be more efficient than querying each time my db.

Thank you!

1 Answer 1

1
  1. you should first make your action in the controller that accept CatalogView object.

  2. To create an object simllar to your C# object you can use this line

    var obj = @Html.Raw(Json.Encode(new CatalogView())
    
  3. Modify your code to

    public class CatalogView
    {
        public IEnumerable<CatalogModel> Catalog;
        public IEnumerable<Device> Devices;
    
        public CatalogView(IEnumerable<CatalogModel> catalog = null, IEnumerable<Device> devices = null)
        {
            Catalog = catalog;
            Devices = devices;
        }
    
        public CatalogView ()
        {
            Catalog = new List<CatalogModel>();
            Devices = new List<Device>();
        }
    }
    

    and make all your public fields public properties in order to be convert to and from JSON 4.for the javascript you can use the following code snippet

    $.ajax({
        type: "post",
        url: "@Url.Action("YourActionName","ControllerName")",
        data: JSON.stringify(obj),
        contentType: "application/json",
        success: function (details) {
            //execute on success
        }
    }); 
    

you should use the http://www.json.org/js.html in order to convert your object to json

Sign up to request clarification or add additional context in comments.

2 Comments

the thing is when I've tried this, I had cycle error(I'm using entity framework). But I used @Html.Raw(Json.Encode(Model.Devices)
Maybe device class has a recursive reference to itself or another class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.