How to pass data from a View to a Model

Asked

Viewed 182 times

2

I searched a lot on Google and Youtube, but I didn’t find anything very clear(I might be searching wrong too). I’m new to Asp.net, and wanted to pass the data from this form (Works.cshtml) to the model (Works.cshtml.Cs) Works.cshtml:

<form asp-controller="Admin" asp-action="Trabalhos" method="post">

                        <h1>Novo<span>:</span></h1>

                        <h3>Título do post<span>:</span></h3>
                        <input type="text" id="titulo"/>
                        <br />

                        <h3>Postagem<span>:</span></h3>
                        <textarea id="textoPost" style="resize:none;"></textarea>
                        <button id="BotaoEnviar" type="submit">Enviar</button>
                    </form>

Works.cshtml.Cs:

namespace Elo.Pages
{
public class AdminModel : PageModel
{



    public void Formulario()
    {
        string titulo = Request.Query["titulo"];
        string textoPost = Request.Query["textoPost"];

        StreamWriter a = new StreamWriter(@"C:\Users\Mateus\Desktop\Nova pasta\linhas.txt");
        a.WriteLine(titulo + textoPost + "<-- deu isso aqui");
    }
}
}

1 answer

3


Your question has some problems and as you said yourself it is very likely that you are researching wrong or something that does not exist.

The first thing you need to understand is the Model, View and Controller. By your question it is possible to see that you are not yet accustomed to the structure of MVC.

The form of his View is sending the data to the action calling for Works of Controller calling for Admin by the method post, according to that line:

<form asp-controller="Admin" asp-action="Trabalhos" method="post">

By clicking the button Put he will make the information Ubmit for that Controller > Admin/Trabalhos, there you will receive the model data and will be able to treat them, as long as it is declared correctly.

Your model Adminmodel, will not receive the data directly from View and the method that made her called Formulario() escapes the concepts of a model, which usually MVC represents an entity of the Database or a representation thereof that will be displayed in the front end for further treatment.

If you follow this tutorial from Microsoft you will better understand MVC and make example by submitting data by GET and POST. Consider studying it! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.