Return View using View Data ASP . NET MVC

Asked

Viewed 360 times

0

I am a beginner in ASP Net and I am in the "studies" phase, I wanted to know why my code is giving attributes errors. I am using a POST method to send the attributes to a VIEW. Here is a picture of the problem attached.

View of my HOME:

@model PostGetModel.Models.Pessoa
@{
    ViewBag.Title = "Index";
}

<form action="/Home/Lista" method="post">
    <fieldset>
        <legend>Pessoa</legend>
        <div>
            <label for="PessoaId">Código da pessoa</label>
        </div>
        <div>
            <input type="number" value="3" id="IdPessoa" name="PessoaId"/>
        </div>

        <div>
            <label for="Nome">Nome da pessoa</label>
        </div>
        <div>
            <input type="text" value="@Model.Nome" id="Nome" name="Nome" />
        </div>


        <div>
            <label for="Twitter">Twitter da pessoa</label>
        </div>
        <div>
            <input type="text" value="@Model.Twitter" id="Twitter" name="Twitter" />
        </div>

        <p><input type="submit" value="Enviar" /></p>
    </fieldset>
</form>

JPG of my problem.

Problema, Controller, View e Solução em execução

This is my first question on Stack Overflow, and I’m grateful to be a part of that community and if anyone can help me. A thousand apologies if I didn’t explain it right. Anything I give an EDIT.

  • 2

    Welcome to the Breno. Some improvements you can make to the question: put the error message and not the image, put the source of the controller and not the image. Always prefer text, when possible.

  • Breno as soon as you make the corrections that George mentioned we can help. I am waiting and help you.

  • Please do not use images to display codes, please read https://pt.meta.stackoverflow.com/a/5485/3635, then after this the error message and code turn into text and edit your question.

3 answers

1

A more interesting way would be to pass the data to the model and retrieve it in the View. Example:

  1. Fill in your list, or your model details;
  2. Then return the values inside the View, return View(obj);;
  3. In the view, refer to the @model for the data model created on the controller;
  4. For components, use Razor, such as a Textbox @Html.EditorFor(model => model.propriedade.

Learn more about Razor.

0

This error occurred because the Idpessoa field is null.

To solve this problem:

1) Make a Model bind with the Idpessoa field:

     <input type="number" value="3" value="@Model.IdPessoa" id="IdPessoa" name="PessoaId"/>

2) I would change the Action List to receive as a Model parameter:

     public ActionResult Lista(Pessoa model)

0

Name of input tag must be equal to parameter in Controller.

Here it is "Personal"

<input type="number" value="3" id="IdPessoa" name="PessoaId"/>

And in the Controller is "idPessoa".

Browser other questions tagged

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