Common methods for data transmission between Controller and View in MVC, mvccontroller
I have been learning MVC over the past few days. By the way, I will record my daily learning experience and share it with you!
In MVC, data transmission between the Controller and View is very frequent, so here we will summarize several common methods I have used in learning:
Forward data from the Controller to the View:
ViewData: a set of Key/Value dictionaries.
Assignment Method (in the cs file ):
ViewData ["Demo"] = "Hello world! ";
Usage (in the aspx file ):
<H1> <%: ViewData ["demo"] %>
Pass data from the View to the Controller:
Method 1: Use FormCollection type parameters to obtain data
Public ActionResult Index (FormCollection collection) {string str = collection ["demo"]; // str = "hello world! "Return View ();}
Method 2: Use Request [key] to obtain data
Public ActionResult Index () {string str = Request ["demo"]; // str = "hello world! "Return View ();}