Two views are located in one ActionResult & mdash; & lt; Youth League committee project & gt; and actionresult Youth League committee

Source: Internet
Author: User

One ActionResult is located in two views-<Youth League project>, actionresult Youth League committee

   

When using MVC for a project, an ActionResult is usually a View, so that the corresponding Return View (); can find the corresponding View below, which is based on a principle, "conventions are greater than configurations", but sometimes we need to jump to different views in an ActionResult to display them on the interface.

Here we do not have to jump to the right view. We can jump to the right view through Return RedirectToAction (), as shown below:

       public ActionResult Detail(Guid id)        {            try            {                var entity = _articleService.GetById(id);                if (entity!=null)                {                                     var model = Mapper.Map<ArticleDetail>(entity);                    return View(model);                }                return RedirectToAction("Index");            }            catch (Exception ex)            {                                throw new Exception(ex.Message);            }                    }

 

This is the list page of the homepage when we judge it as null. You can also add the corresponding Controller in ("Index", "Controller") so that you can jump to the project at will.

Next we will see how to jump to two views.

Requirement: There are two types of projects. The details of a project can be written as one. However, because the content of a project is different, we need to use two views to display the project, two new actions can be created to solve the problem. However, because a lot of code is the same, I think over and over again that a single action jumps to different views for Display Based on different types.

Code:

/// <Summary> /// project details /// </summary> /// <returns> </returns> public ActionResult ProjectDetails (Guid id) {try {var project = _ projectService. getById (id); if (project. projectType = ProjectType. college student project) {var model = new InnovateProjectDetialView (); var entity = _ innovateAndOriginalityProjectService. getById (id); model = Mapper. map (entity, model); return View ("InnovateProjectDetails", model);} else {var model = new AfterSchoolProjectDetialView (); var entity = _ afterSchoolProjectService. getById (id); model = Mapper. map (entity, model); return View ("AfterSchoolProjectDetails", model) ;}} catch (Exception ex) {throw new Exception (ex. message );}}
Explanation: By clicking project details in the previous Index list, we can get different IDs, get the project according to the ID, and then separate them by project category, in this way, you can return to your own views. here we need to add the model to the past. The models on our two pages are different.
  return View("InnovateProjectDetails", model);

The model is not written in the actionresult parameter here because our two types of models are different and need to be instantiated separately, then convert PO-> VO (Here we use autoMapper to achieve high cohesion and low coupling, <Still under study>)

The following is the code to be referenced, because this model is returned from the view.

@model Ylxy.Twdc.Models.InnovateProjectDetialView
@model Ylxy.Twdc.Models.AfterSchoolProjectDetialView

In this way, the effect can be achieved.

As follows.

The general effect is like this.

I want to develop a detailed plan and learn it.

Related Article

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.