MVC design pattern - who loads view initially - asp.net-mvc

This query is about MVC design pattern in general and not ASP.net MVC framework
I understand in MVC (desktop application):
User clicks something in view
this is passed on to controller to manage
controller makes some changes in Model
Model calls method on view which has the logic to refresh UI
Questions around these:
Q1) Can controller also modify View or Model only updates View?
Q2) When screen loads for the first time, there is no Model change. Then, who fetches data from model and populates view? View directly calls Model and populates itself OR controller gets data and passes to view method OR some dummy event is raised at Model which updates View?

From model-view-controller :
The user interacts with the user interface in some way. (for example, presses a mouse button).
The controller handles the input event from the user interface, often via a registered handler or callback and converts the event into appropriate user action, understandable for the model.
The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)
A view queries the model in order to generate an appropriate user interface (for example, the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.
The user interface waits for further user interactions, which restarts the control flow cycle.

In the web context, you always have a request from a web browser. So the logic works as described above but there is a wrapper around it:
Browser sends request to server and waits for reply
The four steps above. In this case, the UI is also an internal model in the server.
The render engine creates a new UI (a new HTML page) from the internal UI model
The server sends the new HTML document back to the browser
Browser renders result

Related

How do I edit a HTML element from a controller?

I'm trying to make it so when I click on a button, a method is called and some text on the page is updated. (The button runs some methods and updates a Semantic UI progress bar with how many methods it's done.)
How would I do this?
I'm using Razor, ASP.NET Core MVC, and ASP.NET Core 5.
Basically it's not a good idea to interact with UI elements in the controller. Doing that you ruin the whole idea behind the MVC pattern a part of which is the Controller.(The controller should know nothing of the view.)
Moreover each execution of a method in the controller represents executing a synchronous request to the server which results in refreshing the page in your browser. Imagine the page refreshes every time the progress bar needs to update. I guess you don't want this behavior. For updating a status bar in the way I think you want it you need to have asynchronous calls of those methods in your view.
If you have to change the UI depending on methods execution status I would suggest using AJAX to make async calls to your methods in the controller and when a response from a finished method comes you update the progress bar.
Hope now you have a better overview. Cheers!
So a possible solution would be:
Click listener in JS , which will send AJAX request to the controller. The controller function should return Non-relational data(preferably JSON). This data that was returned should be catched in the AJAX success function and in this success function you define you HTML change.

Sequence Diagram for Login MVC Webapp

i want to make a sequence diagram, which shows the login process in an .Net MVC Webapp (i.e. webshop).
I am quite new to UML modeling, so i am not sure, how to build the interaction between, Controller, model and view. I found different solution online.
Here are some question i have:
Does the client interact with the view, or directly with the Controller (in my test Trial the client interacts with the Controller)?
For the loginvalidation: In my test Trial the Controller ask the model, if the login_data (username and pw) are correct. Is it necessary, that the model interacts with an database, where the user data are stored?
If i would like to send data in an http Request, should i just add the variable in the brackets?
Here is my test Trial:
This is perfect. You just could shortcut the http-response and move it outside the alt fragment.
The controller is the one to "do the job" and the view just to present it. Actually there's a bit of intermix since views contain some basic I/O logic. But here the http-data travel from the client to the controller.
It depends. You "can" show that but you "must not". If the model reader needs to know the details you can show that directly or in a separate SD.
You would usually pass data as parameter of a method. You can also show concrete data (e.g. a quoted string or an integer value).

How do I record the request used to call an Action

I'm working on an existing, fairly labyrinthine, intranet that I do not have authority to re-factor.
The back button is disabled in this application so back navigation is through code.
This was OK when we had simple workflows e.g. Index->Details->Edit
However, the workflows are now becoming longer and crossing between different Controller domains so managing back navigation is becoming more complex.
I'm thinking that if I have an object that records each step forwards then I can simply replay it backwards. This should accommodate any edits made downstream in the workflow.
To do this I need to record the URL or at least the Action, Controller and parameters sent to each Action into the object.
My questions are:
When an action/method is called is there a object that contains the call so that I can record it?
If not, can I find out which Action and Controller I am currently in using code rather than having to explicitly set these into the recording object.
I'm using ASP.MVC 5

C# MVC Create Form

I am using C# MVC. I have created a forms before where all of the data needed is on one form. Once the user fills, it then goes to the controller which inserts the data into a database table.
How would I handle a situation where the fields are on 3 different pages. On the 3 page, I like to submit. How does the data presist?
Also is a session variable Ok to use for data that is shared with many pages?
You could use different views, or use one view with a tabbed UI, and use three partial views as the content for each tab as an alternative.
You can use session, but then you have to ensure they can complete the content within the 20 minute time limit; as long as a postback occurs, then that limit is reset.
If your doing a wizard style workflow you could just have a ViewModel for each page and as the user progresses through to each page store each ViewModel in a Session variable. Then upon complete you could grab all the Models out of Session then aggregate the values into your completed form state.
Another way would be, for each POST call service that persists the page data into a data store so as the user navigates through each page, the entity is populated and stored on your data base via some service.
Could just have one partial view to spit out one single ViewModel that has properties for all 3 pages, and then use jQuery wizard plugins (or maybe you're writing your own) to simple manipulate the DOM to show/hide each page as the NEXT/PREVIOUS buttons are clicked. It will all still be part of one single FORM. And then hitting submit would post back to your controller method, say with a $.ajax() POST with serialized JSON.
If you shared a view model between all 3 of the pages in the Wizard, you could deploy HiddenFor fields on the pages to retain information that is not visible on that page of the Wizard.
This would persist data fields entered on other forms throughout your wizard and does not rely on the Session to store the information.

How to persist a model across multiple requests in ASP.NET MVC 2

I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!!!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
Thanks in advance for the ideas!
I would probably have the partial view send it's data to the main page (with javascript). That way there is only one post to the server and it is when all of the data the user needs to enter has been filled out. How are you displaying the partial view? Is it on the main page (in a div), or is it a separate pop-up window? Either way, you should be able to use javascript to store this data on the main page and post all of the data back at one time.
HTH

Resources