MVC3.0 different view layers call the same Controller method, but the parameters are different

Source: Internet
Author: User

In the view layer, go to the detailed page and view shujia 1 ., However, you do not need to add 1 to the modification page. Both call the controller to retrieve a data record from the DB!

On the details page:

"InfoContent", "Activity", new {id = x. NOTICE_ID, browseNum = "add "}

On the modify page:

// Modify
Function Modify (value)
{
Location. href = '/Activity/InfoEdit? P_strNoticeID = '+ value +' & browseNum = '+ unadd;
}

In the Repository

Repository Code

1 public ACT_NOTICE GetSingleActNotice (Guid p_Notice_ID, string browseNum)
2 {
3
4
5 ACT_NOTICE notice = Single (x => x. NOTICE_ID = p_Notice_ID );
6 if (browseNum = "add ")
7 {
8 notice. BROWSE_NUM = notice. BROWSE_NUM + 1;
9}
10
11 SubmitChanges ();
12
13 return notice;
14}

Personal conclusion: the idea of passing parameters extends to controlling the display content by passing different parameters. For example, the same view layer previously created does not display images either, the other does not have any image. we can define a field in viewmodel as a flag.

/// <Summary>
/// Control the parameters of the image displayed on the page
/// </Summary>
Public string showDiv {get; set ;}

In controller

Controller Code

1 /// <summary>
2 // load the announcement list on the Index page
3 /// </summary>
4 /// <returns> </returns>
5 public ActionResult List (int flag)
6 {
7 NoticeViewModel viewModel = new NoticeViewModel ();
8 viewModel = _ noticeLogic. GetNotctForList ();
9
10 // flag = 1 display the picture of the announcement on the index page
11 if (flag = 1)
12 {
13 viewModel. showDiv = "show ";
14}
15
16 return PartialView ("List", viewModel );
17
18}

In this way, you can pass parameters on the Index page.
<Div class = "news">
@ Html. Action ("List", "Notice", new {flag = "1 "})
</Div>

The parameters passed on the other page are different.

At the View layer, the system determines whether to load the display of the div with an image when loading the page.

@ If (Model. showDiv = "show ")

{

Display the image on the index page (parameter 1)

}

Else

{

Do not show images on other pages

}

It can also be extended to viewBag defining a parameter in the controller and passing it to the view layer!

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.