Go ASP. 5-pass data from the controller to the view

Source: Internet
Author: User

Before we discuss the database and the data model, let's talk about how to pass data from the controller to the view. The Controller class responds to the URL of the request. The controller class is where you write code to process incoming requests, retrieve data from the database, and ultimately decide what type of return results will be sent back to the browser. The view template can be used by the controller to produce formatted HTML to return to the browser.

The controller is responsible for providing a required view template for any data or object, using this view template to render the HTML returned to the browser. As a best practice, A view template should never execute business logic or interact directly with the database . Accordingly, a view template should only interact with the data provided by the Controller. Maintaining this " isolation relationship " can help keep your code clean, testable, and easier to maintain.

Currently, the  HelloWorldController action method in the class Welcome requires one name and one numTimes parameter, which is then output directly to the browser. Instead of just returning a string, let's change the controller to use the view template. The view template generates dynamic HTML, which means that you need to pass data from the controller to the view in the appropriate way to generate dynamic HTML. You can put the Dynamic Data (parameters) required by the view template into an object in the controller ViewBag , and then the view template can access the object.

Open the HelloWorldController.cs file, change the Welcome method, Message and NumTimes Add the value to the ViewBag object. ViewBagis a dynamic object, which means that when you don't give a ViewBag placement property, it doesn't have any properties, and you can put any object you want to put  ViewBag in the object. The ASP. NET MVC Model binding system automatically maps the query string in the URL in the address bar to the parameters ( name and) in your method numTimes .

The complete HelloWorldController.cs file is as follows:

Using system.web;using system.web.mvc;namespace Mvcmovie.controllers{public class helloworldcontroller:controller{ Public ActionResult Index () {return View ();} Public ActionResult Welcome (string name, int numtimes = 1) {viewbag.message = "Hello" + name; Viewbag.numtimes = Numtimes;return View ();}}}

The ViewBag object now contains data and is automatically passed to the view template. Next, you need a welcome view template! From the build menu, choose Generate Mvcmovie ( shortcut key ctrl+shift+b) to ensure that the project compiles successfully.

On the Views\helloworld folder, right-click add ( view ), select MVC 5 View Page with (Layout Razor).

In the specify item name (Specify name for item )dialog box, enter " Welcome" and click " OK" /c18>(OK)".
In the Select Layout (TheSelect a layout page)dialog box, accept the default layout _layout.cshtml ", and click OK (OK).

The mvcmovie\views\helloworld\welcome.cshtml file was created successfully.

Replace the tag in the welcome.cshtml file, and you will create a loop that loops through "Hello" multiple times.

The complete welcome.cshtml file is shown below.

@{viewbag.title = "Welcome";} 

Go ASP. 5-pass data from the controller to the view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.