Where to store the state in MVC model - model-view-controller

I'm writing an infinite scrolling list. There is a data grid that displays the data and a controller and model that provides the data to this grid.
Right now, the view receives the list of data and displays it. For the sake of infinite scrolling, it throws an event which lets its parent know that it needs the next page of data. The parent requests the next page of data, and it's the controller that decides what is the next page of data (in terms of page number, page size, etc.)
I'd like to know where I should keep the page number and page size, or in general, using MVC, where are we supposed to keep the state ?

Related

How to display unique nodes for a page?

The scenario is like this: we have a news website, every page display different type of news nodes. We like to set the in this way that, for a single page, no news node should be display more twice. i.e. should not be duplicate.
We are creating paragraphs, which are fetching different type of news nodes based on taxonomies. For a single page, user can add as many paragraph as they want. Each paragraph display nodes with different layout. Paragraph fetching nodes by entity query and views as well.
So question is, how we can restrict nodes, so that if one displaying in one paragraph, it should not display in another paragraph for a single page?
Something like, if we can create a singleton class or static function, so on page load, it create a blank array, and each subsequent call of that function will fill the array and compare with the old list which are already dumped or added. But I am not sure which hook should be used for page load and how to handle the views query.
Beside this should be session specific and also keep it mind about the performance.
Does anyone has any idea on this? Surely its complex.

MVC3 Display search result on the current view (without any new view created)

I am pulling some data from a databse and displying it in a list like in this example. I want to implement a search on the top of the page to search the value of the data reurned on the page. This example creates a new view for the search page. Is it possible to implement a search on the current view without creating a new view for the search results.
I don't mind doing it with a new view, but I am just wondering
The view doesn't need to care about what you are doing in the background with the data. It simply displays whatever you are sending to it in a formatted pretty way. Because of this there is certainly nothing wrong with supplying a parameter in a search box and then pushing this to the controller when clicking a search button. The controller will then be doing the requests to your model or repo to filter the data culminating in a return of your view loaded with the data it has found, exactly as it would on your initial load (just now with less data than it did before). There should be no need to create a totally different view for this, the view is just your template for returned data.

Rendering Image without postback in MVC from cache

I have a dropdown, that contains some list of StudentCode...
when user select any studentcode student data should be displayed in form with student's Image...
for displaying Student Image, Im putting student image in cache memory... and I have one Iframe that gets the byte data from cache memory and render that image on my page... problem is... this thing need at least 1 postback...
can I make some partial postback to render this image?
I see couple problems with the implementation.
1) You should avoid using IFrames in general as there are better ways of handling rendering parts of the webpage.
2) Storing images in cache means that you actually downloaded all of them for each Student in the dropdown list and placed them in the cache. What if there are 100 students? Getting images for all of them and storing them might take a lot of resources.
I suggest you to do the autopostback on dropdown selected value change (via ajax call) which calls a method that will return a partial view with all the information you need for the particular student, including the image.
I found using Ajaxify library (http://max.jsrhost.com/ajaxify/) very easy and convenient for ajax calls.
You can do the same thing without ajax at all in which case the whole page will reload with the same result.

get and display data in MVC 3 using Ajax

I have one view page in MVC named as ‘Index’. There are 3 main controls in that view page. For better understanding, take below example as reference.
1. One Dropdown ‘Customer’
2. One Dropdown ‘Project’ (that depends on ‘Customer’)
3. List of Task (that depends on ‘Project’).
Currently first user needs to select ‘Customer’. So based on customer ‘Project’ dropdown data will be fetched from post method of Index and bind that ‘Project’ dropdown. After selecting ‘Project’, List of task will be fetch from post method of Index and bind those tasks. So every time page is loaded again.I want to do it by Ajax form submit.
My problem – those 3 controls are in one view page so I want to return page view using Ajax without using Partial View. Is this possible? If yes then how? Or any other solution so that I am able to display data without reload whole page again.
If u can give me example of such scenario then it will be good.

Should navigation logic be in the controller or the view?

In an MVC web app, where is the "right" place to put the code/logic to display a url link to a "next page" navigation control, the controller or the view? If I put it in the view, I have to pass to the view not only the data to be displayed on the current page but also data relating to the next page aka the page id of the next page. If I put it in the controller, the controller has to be aware of the navigation that the particular view is going to display. Neither approach seems very elegant to me. Is there another way?
I don't know if there really is a "right" way to do this. Here's some different ways that I would think about it though:
if a page of items is itself an object in the system, then the controller makes sense
if a page only exists within the view logic, then the view makes sense
if the view receives the entire list, then pagination is a presentation concept
if you foresee the need for different page sizes or organizations, then keep the logic out of the controller
My gut says that you should try very hard to keep all of the pagination logic in the view. This usually means that you want some way to calculate what the next page is based on the current page in the view or make the controller have some concept of a starting point in the result set. I usually do the latter - the view retrieves the data with an optional starting point that is the ID of the last item on the previous page. This way the pagination logic is in the view and the data retrieval is simple.
One of the things to watch out for is how you will handle retrieving the next page of data if the item that you are keying on no longer exists. In other words, if you link says "the first page following item A" and "item A" has been deleted, then you need to do something reasonable.
The controller should provide meta information about the data being displayed: number of pages and current page index are two that get what you need here. The navigation control should encapsulate the logic which, given those two pieces of information, can render the rest.

Resources