How to pass model data in to generate razor view - asp.net-web-api

I am trying to figure out how I can pass model data from a controller to a razor view in a non-mvc Razor Pages web application.
We have a system in process where we currently generate a model, and send it off to an external service which will bind a razor template with the model and send an email. We need to migrate away from the external service and switch over to our existing mail service. I already have the model ready to post to an endpoint, and I have the code that will send an email with the html body. All the examples I have seen for Razor Pages either generate the model in the cshtml.cs file. I can create a web app that has a controller with an endpoint where I can post the json model (which I deserialize to a custom class), and I have razor pages that can bind to my custom class. I just can't figure out how I can go from the controller that receives the request, to passing that data to the Razor page to render the view.

Yeah, after hours of searching I posted this and was presented with a "Does this answer your question" suggestion before posting. I was "No, that is not it." After posting and re-reading, yeah, that was it. :(
Answer here:
How pass objects from one page to another on ASP.Net Core with razor pages?

Related

MVC pattern: js code that is calling server via ajax part of view or controller?

I know that the webpage that is opened by the user is part of the view. This one is easy, but what about js code calling the server to retrieve data via ajax? Is this part of the controller or still the view?
Can the view request data like this?
Thank you very much for any help.
Javascript, including Ajax, is a client-side technology. Therefore, in the context of MVC, any js script must be part of a template file.
Note that a template file is not the view (the V in MVC), but a part of it. The view should incorporate both server-side components (classes, interfaces, etc) and client-side components (html, js, css, images, etc). For example, an instance of a view class could read some data from the domain model and then load and render a certain template file, injecting the fetched data into it (formatted), in order to be printed on the screen.
So, an ajax object should be defined in a template file. Its request to the server is handled either by the controller, or by the server-side components of the view, depending on the MVC approach that you are choosing to implement. Though, the server response should always be created by the server-side components of the view.

In ASP .NET Core Razor Pages, how do I properly present server responses to the user?

Recently I have started to deal with dynamic content in my RazorPages web application. I found out about AJAX and have started to incorporate it into my application since it helps me display information to the user. For example, when a user presses a button, some information is taken from an input field and sent to the server, as usual. I use the server response (OnGet or OnPost return value) to display information to the user without them reloading the page.
However, I cannot figure out how to incorporate model binding into this. When I use AJAX I can build the message myself but the model binder of RazorPages was made to this automatically - which is great and I use it a lot, but how can I access the return value of my OnGet or OnPost when I created a form and used tag helpers to allow model binding? I know that I can return Content(string) in these methods but that will show an empty page with the string I return, of course.
My question is: How can I access such a string to display it to the user? Am I even on the right track here or is there maybe a better way to solve this kind of issue?
I would recommend checking out some of the Razor Pages tutorials as they cover the basics of posting form data using Razor pages:
https://www.learnrazorpages.com/razor-pages/forms
Best of luck on your programming journey.

Two forms (one in layout and one in Page) how to handle in asp.net mvc3

I am working on a mobile website in ASP.NET MVC3. I have a page where i have search bar in my header. this header is coming from my Layout page which is common to all other Views. And inside my specific page, I have page specific content(forms).
For my Customer/Add action, I return the Add View of Customer which is strongly typed to my CustomerViewModel. I will have form tag in my Add View which will be posted to HttpPost Add Action method when form gets submitted. That is fine. My question is how will i handle the search box content ? I believe only one form is allowed in the page.So if i have a SearchViewModel which is binded to my Search View(Partial), It is going to be 2 forms in my page. So i can not do that.
I can handle the search part by reading the content in java script and calling another action to get Search results.Is that the only way to do that ? I am worried about those devices where java script is disabled. What should i do ? Please advice
No. You can have more than 1 form on the page. In fact, here you should. Your Add Customer page should submit to 1 action method, but your search form should submit to a different action method.
If you are familiar with webforms, that framework only allows you to have 1 form on the page, but not because HTML requires it. Webforms requires it because it is the only way the framework can carry all of the data from various server controls across POST requests (using ViewState). Webforms has historically not been very HTML or HTTP friendly.
You just can't have forms nested within other forms in HTML, but it is completely legal (and recommended in MVC) to have more than 1 form on a page.
As for AJAX, I would not worry about devices that do not have javascript enabled. There are only like 6 or 7 people on the planet that don't have javascript on their web devices, and if someone disables javascript, they won't be able to experience 99% of the rest of the web anyhow.

Add Live Support section to mvc 3 project

Greeting to the community. This is my very first question.
I had in mind to add Livezilla live support in my MVC website, but I realised that there are some problems.
First of all, it is written in php and mysql, so is it possible to comply with MVC and sql server?
Does anybody know a similar solution (like livezilla) in my case?
PS. Excuse my poor english...
you could even look into using an iFrame to run livezilla inside of an mvc3 page if you wanted.
or, you could roll your own. if you wanted to try implementing a live response mvc3 page look into jquery and ajax asynchronous calls such as $.post and also look into the extension for controller AsyncController, and [AsyncTimeout(XXX)]. you will also have to read up on sending and receiving json objects in ajax and from a controller to the view.
Simple psuedo:
View:
setTimeOut calls a javascript function every X milliseconds
the javascript function uses json to wrap page data and pass it to the controller inside a $.post() call
Controller:
recieves page data as an model object in a post
determines if page is out of sync or not
returns relevant sync data in a json object back to the view
View:
the same javascript function receives the json object from controller
updates the page based on relevant sync data
Problem solved. I just uploaded the folder given from livezilla in the root folder of my website. Then the only need to met is to have a Mysql server to create the database for livezilla. After that php and mvc runs together with no problem.

Web app entity 3 step creation with MVC(either asp.net or rails)

Let's say I have an Employee, and for the creation of such employee in my web application I want to follow the next flow.
Create <-> Verify -> Save
In the create page the user can set up a bunch of properties,
In the verify page the user is presented with two options "make changes" and "verify"
In the save page the user is presented with a confirmation page
I have two approaches to this:
Use Javascript to change "action" and "method" of the form.
Handle the logic on the action
(which feels kind of clunky at least
with asp.net mvc)
Which do you prefer ?
Is there a better way to do this?
Pretty easy in ASP.NET MVC. See these posts for guidance:
how to make a wizard with ASP.Net MVC
http://shouldersofgiants.co.uk/Blog/post/2009/09/16/A-RESTful-Wizard-Using-ASPNet-MVCe280a6-Perhaps.aspx
Personally, I would do it as a single GET/POST and utilize some basic jQuery to show/hide DIVs containing the necessary inputs as noted in the first link.
I would do this with routes
GET new to render the create form
POST new to show the verify form
POST create to actually create the resource
Create <-> Verify -> Save
It isn't clunky, Create is Get, verify is post of the same URL. The method that handles the Get and the one that does the post use the Same View, just with different info sent to it.
So for the create the view is instructed to post to the same Url, when u are already to verify it will post to the Save action.
The above works for any non js client. You can then hook some js, so instead of posting the form from Create -> Verify, you would change in the client side. This way it works for both js - non js versions. You can even display the Save confirmation with the same technique if you wanted. Progressive Js.

Resources