The method of transmitting value between controller and controller to view in MVC

Source: Internet
Author: User

Since doing the examination, and other people have also exchanged, found that the system internal value is a more laborious problem. This blog explains the two values in the case of--MVC, which includes the second controller-to-view transfer value.

Example: I have two controller:c1 and C2, and I want to upload the course ID of the course entity in C1 to C2. The plan is to upload the course ID in C1 to the address bar of the C2 corresponding view page, and then C2 from your view page.

Look at the code:

The following is a Easyui table that executes the Queryallexaminfo method under Scoringmanagementcotroller
<table id= "DG1" class= "Easyui-datagrid" style= "Width:auto; Height:auto; Url= "/scoringmanagement/queryallexaminfo" title= "Query Results" data-options= "Fitcolumns:true,singleselect:true" sortname= "CourseID" sortorder= "ASC" rownumbers= "true" pagination= "true" >                <thead style= "width:420px; height:auto;" >                    <tr>
                        Here is a table cell column, click on the content below this column, execute the TEST1 function
                        <th data-options= "field: ' Operation ', Width:80,align: ' Center ', formatter:test1 ' > Start enigmatic grading </th>                    </tr >                    <script type= "text/ecmascript" >                        function test1 (value, row) {                            //go to the next page of the link address, which contains the ID of the selected exam account                            return ' <a href= '. /questionselection/index? Courseid= ' + row. CourseID + ' > Start enigmatic grading </a> '                            $ (' #dg1 '). DataGrid (' reload ');                            $ (' #dg1 '). DataGrid (' clear ');//Clear Table traces                        }                    </script>                </thead>            </table>
This code shows how to CourseID the field values in C1 to the C2 view in the browser's address bar.

below to see C2 how to get their own view page Address bar CourseID, here is twists.

1. First get its value in the C2 default action:       

        Public ActionResult Index ()        {            viewdata["Id"] = request.querystring["CourseID"];  Course ID, get                        return View () from the link address on this page;        
2. Upload the acquired value to a hidden text box in the View view       

        <input id= "a" type = "text" hidden= "hidden" value= "@ViewData [" id "]"/>
3. Other action in C2 gets this value     

                                $ (' #cboQuestionType '). ComboBox ({onClick: $ (function (Cbox) { var CourseID = "@ViewData [" Id "].                            ToString () ";                                $.ajax ({type: "POST", Async:false, URL: '..                                /questionselection/[email protected]["Id"] ', data: {"CourseID": CourseID},                                    Datatype:json, Success:function (data) {                                    $ (' #cboQuestionType '). ComboBox (' Clear '). ComboBox (' LoadData ', data);                        $ (' #cboQuestionType '). ComboBox (' SetValue ', ' Please select Question ')}}) })
This is a piece of JavaScript code that executes the Queryquestiontypebycourse method by clicking the drop-down box, which is in the Questionselection controller.

The value of the CourseID is first obtained and then sent to the background controller via data.

4, the last C2 get CourseID        

         The course ID transferred back from the front desk is  stored in the course types Entity         examcoursequestiontypeentity Encoursequestiontype = new Examcoursequestiontypeentity ();         Encoursequestiontype.courseid = request["Id"]. ToString ();//course ID, obtained from the hidden field of the page

Summary: Here the value of the general is more troublesome, there is no better way to find, if you have good suggestions welcome message ~ ~ These methods are required to find and summarize a large number, coupled with the usual multi-exchange and accumulation.

The value of the transmission itself is not difficult, the more difficult is the value of the skill and innovation, where to pass, in what way is a very test programmer to consider the flexibility of the problem.

The method of transmitting value between controller and controller to view in MVC

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.