Share via

general question on Controllers and Views

elsvieta 416 Reputation points
2023-12-08T16:34:24.3566667+00:00

hi all,

I have a sample project (NET 8, ASP.NET Core MVC) and I added a few Controllers and Views.

Controllers: Home Controller, Group1Controller, Group2Controller

Views: Home folder: Index.cshtml, Privacy.cshtml
Group1 folder: Index.cshtml
Group2 folder: Index.cshtml

Models folder:
BaseViewModel.cs
ErrorViewModel.cs

and Program.cs directly under the project folder

Let me focus on Group2:

I should have a number of labels in the page and further down the page I should have two text fields for input, a SUBMIT button and a CLEAR button.

On the click event for the SUBMIT button, I should have a number of fields filled with some data coming from a database.

How does this sequence of events break down in terms of controllers and views.

The way I understand MVC is that all logic will be in the Group2Controller, but I am not grasping the logic of it. In Framework I had the PageLoad method and I would cram all logic in there: if(!IsPostBack) then clear all fields, make controls appear, disappear, enable, disable, etc.

In MVC it seems I render a new view at each action?

So, for start, what is the best practice for having just this behavior implemented:

I have my 2 input fields, my SUBMIT and CLEAR buttons and a few readonly text fields that are supposed to be populated with some values coming from a database (all data access is implemented).

How do I implement the simple action of clicking the SUBMIT button and have the fields populated and then having them cleared and re-submit and so on?

Thanks a bunch,

elsvieta

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 83,581 Reputation points Volunteer Moderator
    2023-12-08T22:30:31.7633333+00:00

    webforms used a control tree.

    • in the on-init built a control tree from the webform.aspx template.
    • then it loaded any postback data to the control tree.
    • then it fired unload, when you could update the tree.
    • then it faked click/submit events based on postback values
    • the control tree is then rendered to html

    MVC is a simpler request / response

    • routing picks controller action
    • an instance of the controller is created
    • any postback data is bound the action parameters
    • the controller action is called and returns an action result set (often a view)
    • the action result is called to produce the response (typically html)

    with mvc you can have separate get/post actions or use the same code. html submit button pst their name and value, so an action can detect which button caused the postback.

    razor views are template engine, not a component tree.

    if you are moving to net 8 from webforms, you probably should pick razor pages. while the processing model is similar to MVC, it simplified to a view and optional code behind.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.