Pass data from cs to cshtml MVC

0

I manage a project which must generate the body of a personalized email message (HTML) for this I have an auxiliary .cshtml file which is taken as the body of the message. To fill this body in the controller I have a class that obtains the selected object.

static CURSO cursoSelected = new CURSO();
public static CURSO cursoSel()
{
    return cursoSelected;
}

But I'm not sure how to call it and use it in the cshtml. I have this idea but it does not work

@model Proyecto.Models.Historial.cursoSel()

and in the section

<body>
    <label style="font-size:18px">Curso de</label>@Proyecto.Models.Historial.cursoSel().NOMBRE_CURSO< br />
</body>
    
asked by Kevtho 03.07.2017 в 20:09
source

2 answers

0

I'm not sure which elements you want to communicate data between. But following the guidelines of the MVC pattern, you need a controller to evoke the view where you will show the data you need, sending them as a parameter.

For example, if you had a controller with a GET method like:

    public ActionResult cursoSel()
    {
        //Aquí la lógica para seleccionar el objeto que requieres enviar a la vista.

        return View(cursoSelected);//cursoSelected del tipo CURSO
    }

Next, the view that the selected object receives would be: cursoSel.cshtml . In it you should define the model that uses: @model Proyecto.Models.CURSO

So that in your body you could retrieve information about the object in some way like: @Html.DisplayFor(model => model.Nombre_Curso);

There are also other ways to pass objects to a view, or to the master view. To mention, using ViewBag, ViewData, TempData or variables of the Session type (surely many more ways that I ignore).

Controller.View Method link

I hope it helps you. Greetings.

    
answered by 03.07.2017 в 20:57
0

I'm not sure if this is what you need to do, but you could try the following:

In the controller have an ActionResult defined that returns the body of the message:

public ActionResult cursoSel()
{
    //Aquí la lógica para seleccionar el objeto que requieres enviar a la vista.

    return View(cursoSelected);//cursoSelected del tipo CURSO
}

Then, in the view, to obtain the necessary data, you make a PostBack to the controller, to the specific function:

<body>
<label style="font-size:18px">Curso de</label>@Url.Action("cursoSel")< br />

Where "cursoSel" is the name of the action within your controller.

    
answered by 03.07.2017 в 21:25