Doubt with View Tipada?

Asked

Viewed 562 times

0

I got the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PostGetModel.Models;

namespace PostGetModel.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            var pessoa = new Pessoa
                {
                    PessoaId = 1,
                    Nome = "teste teste",
                    twitter = "@teste"

                };

            return View(pessoa);
        }

    }
}

on the Index :

The code does not complete in this part where I add :Postgetmodel what could be wrong?

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

<h2>Index</h2>

<p>model.PessoaId</p>

inserir a descrição da imagem aqui

  • Does the system raise an exception? What is the name of your view?

  • The view name is index

  • 1

    The problem is in this part: @Model Postgetmodel.Models.Pessoa @{ Viewbag.Title = "Index"; } <H2>Index</H2> <p>.Personal modelid</p>

1 answer

1


Itasouza,

In this part of the code

<p>model.PessoaId</p>

To access your model, you would need to do it this way:

<p>@Model.PessoaId</p>

This is because by placing an element without "@" inside an HTML tag, Razor will interpret this as an HTML code, not a C code#.

If the View name is index, as you indicated, it is necessary that it has the same name as your Action, in this case, Index, inside the Home folder. However, if the exception raised is in the View, then you are successfully accessing the page.

As also indicated by Leandro, the directive that indicates which is the Viewmodel of your view should always be minuscule, while to access the Model attribute of your view, this should always be capitalized.

  • Friend Vinicius, your tip was important to me that I’m starting in MVC, but the problem is in : @Model Postgetmodel.Models.Pessoa is not recognizing this.

  • I understand. You can post the exception?

  • 2

    Look at the picture!! I thank you!!

  • 2

    The directive @modelwhich informs the type used by the Model should always be lower case, in case the one used in the first line of your View

  • 2

    I thank you all! Vinicius, you were right about :@model Postgetmodel.Models.Pessoa , is as @Model and so did not recognize, but now I already understand better about View Tipada , Viewbag, Viewdata

Browser other questions tagged

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