ASP.NET MVC ViewModel and Request Variables - c#

Greetings!
I need assistance with ASP.NET MVC and the values posted from a form operation (form is inside a Partial View).
On my page to create a User Account, various form fields collect the information. The Partial View is backed by a ViewModel (UserAccountViewModel).
To validate and/or save the information, I need to pass the values of the form to UserAccountService. Back in Java and Struts 1.x, I used the getMap() method of the DynaActionForm, but being an ASP.NET newbie, I'm not sure of the best way to proceed.
On a post operation, are the fields of the ViewModel automatically updated? If that's the case, I could pass the ViewModel to the Service layer (not my preferred solution, but good enough).
Jason

You can use the UpdateForm method in .net to have it automatically map the form data to your model based on similar naming.
UserAccountViewModel.UpdateForm(Request.Form);
UpdateForm will work on pretty much any class with properties, you just need to import the namespace. Here is a pretty good tutorial on form handling in asp.net mvc that uses this method. This sounds like it's a similar method to what you're used to in Java.

Related

How to separate the view and the controller in an ASP.NET application

I have an ASP.NET application that was developed by "programmers". This application contains all things which you should not do:
Hardcoded settings
Copy/paste anywhere (code not re-used)
Make a lot of small SELECT requests to the DB for each row instead of doing JOIN
Model, view and controller in one function
Etc.
Now, my goal is not to throw everything away and start over, but I want to separate different aspects of the MVC of the application. I do not want to start a new MVC project, I would like to evolve the existing solution into something modular.
For the controller, there is no problem, I can create classes that will manage DB connections, send mails etc. On the other hand I do not know how to separate the view and the controller.
The problem that traditional ASP pages myPage.aspx have an associated file myPage.aspx.vb and in this vb file there are both view management part(page elements, like dropdowns) and also the Business part (controller) which is executed on the button click.
I thought about making a call to a myPageControl.vb class that will contain the business part from the file myPage.aspx.vb, which will call the Model (db, mail, other).
(View: myPage.aspx.vb) -> (Control: myPageControl.vb) -> (Model: Db.vb, Mail.vb)
The problem is: how should I do to modify the page content from the controller, for example change a list value or display a text on it. I have to make a call to the View (to the other direction) and pass by parameter the class MyPage (.asp.vb)
I have already tried to find an answer to my question, but I've found only answers taking about MVC projects.
Does anyone have any idea how I should do it?
Seperation of Concerns was one of the main problems with webforms, and one of the advantages of MVC. In my opinion the best you could probably do is seperate the business logic into classes like you are doing now so code could be reused throughout the application, but completely "seperating" everything may require rebuilding the application as an MVC app.
The only answer I've found to this is to have the controller send the "data to bind to" to the page as XML. then all the page has is its page_load of course, and then a method to receive the XML and update itself from it. You can use smart naming structures so that you can do reflection and autobind from the xml to page elements.
Then on an action, have the page generate an xml of all the elements on it and their values, and send that through a "ProcessAction" method that determines the correct controller and calls the right method on the controller.
But as suggested, doing it over as an MVC project probably makes the most sense if that's the pattern you are going for. What I suggested works, but it will be as much or more work than just starting from scratch with MVC. Besides, remember that "support" for web forms is disappearing soon. It's not in (and won't be in) .NetCore, so it's days are numbered.

pattern best way to connect View to multiple Controllers?

I'm recently doing a purchase order program using the MVC pattern in C# (not ASP.NET web app, just a desktop program). Now the problem is that in the UI there are lots of buttons that will link to another section, let's say from the order page I can click and go to the inventory page. Each section of course have their own set of MVC.
How can I connect the button to other controller while maintaining abstraction? That is each view does not have to understand how the other controller works, or what parameters it requires to work.
There are a couple of design possibilities that comes to mind but I have no idea which one is better / simpler:
The dirty way: just put the references to all the required controllers in one view, but this way reduce abstraction.
Shared view space: each button is a different view that has their own controller, but then I need to have view manager of some sort.
Routing: pretty much like ASP. NET RedirectToAction, make a custom router that use common type (like string) to determine which controller and which action to perform
Custom button for each action: for each controller and each action I make a button class just for it. Then I need a way for the view to supply the required parameters to each button.
Any suggestion on what should I do?
What i think that you should for the "shared view" i.e create on shared view containing all of that buttons and their calls if this is not working for you then you can create a saprate calls of WebAPI in their controller to get your date in return.
I decided to use a modification to my 4th option, a factory pattern that creates all my buttons. The factory is given the references to all the controllers and initializes all the buttons accordingly.
The view only need this factory during construction and ask for any of the button it can produce.
This way the view doesn't need to know anything about the controller during construction. The controller also doesn't need to know the view at all.
Thanks for everyone sharing their opinion.

Injecting Partial View Dynamically

I am developing UI for a dynamic platform, a user can add his/her own properties to classes in platform. I will go through an example to make my question clear:
Say we have a class X with ID and DisplayName properties. These are default initial properties of class X. In the ASP.Net web Application I have created an associated form for class X with both properties bound to model. Yet users can add fields to this class and I want to create associated sections in the UI, but with condition that the user can offer correct HTML (or aspx) markup for new field(s) and the markup would render in the UI. This is to avoid changing code for little domain manipulations and does not require code recompile and server restart.
Said Otherwise, I want a functionality like saving partial views in the DB and loading them in the UI.
I have found this Question so close to mine, yet the provided answer is not good enough.
If you can point me to any project, tutorial or sample code, I will appreciate it.
What I have done so far: I have checked Naked Objects for MVC, yet I think it is an overkill for my question. MVC Form Factory is very interesting but I need those added variables be accessible using provided template.

C# MVC and API in one project

I'm going to create a C# Web Application using MVC. I also want to have an API which mirrors the Web UI. As an example: if I have a web ui to "Create employee" I want a matching API call which does the same. It would take in the same information and follow the same process. I want to avoid duplication of code, so I'm looking for guidance on how to best structure my VS Project, so that I start the project correctly. Where should I put my models, my controllers etc? All suggestions appreciated.
I would use ViewModels for the Views and the Api. This viewmodel is used to return to the browser as JSON or to build the View.
I would then use a library for the code handling and redirect to the libray in the Action methods in the view controller.
The View controller will recieve the ViewModel from the API or normal Action methods. It could pass this viewmodel along to the library or possibly translate it to a datamodel and then pass it to the library. In the same action method the view controller will get a model back from the library and rebuilds the viewmodel.
If you use a ViewModel the Api can easily translate it to and from JSON.

ASP.NET MVC: How to pass Dictionary<string, string> from controller to jquery for use?

As I am quite green for ASP.NET MVC so maybe my question is very simple for many experienced users.
I am now implementing a web-based platform and I am trying to pass some data in Dictionary from Controller to View using ViewBag.
However, I am not able to use those ViewBag in jQuery,
when I "alert" them, they become a series of [Object, Object].
So I would like to ask how I can retrieve those data in jQuery?
Thanks.
You can serialize a .NET dictionary to Json. The Json.NET library has first class support for this
just do a loop to go through dictionary in your view to bind to your controls. However, try to limit your business logic in views and keep in controller .
Or I am misunderstanding your question. Any specific example would help.
You could use jQuery Ajax to get this data from an action that returns the data in a JsonResult. While the other two approaches should work this approach does not require to load the whole bunch of data with the page and make your UI more responsive through Ajax.

Categories