ASP.net MVC error

Asked

Viewed 87 times

0

I have this code, working perfectly as I wish

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<div class="container">
    <form class="well form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ID_Reserva)

        <div class="form-group">
            @Html.LabelFor(model => model.ID_Cliente, "ID_Cliente", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ID_Cliente", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ID_Cliente, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DataEntrada, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DataEntrada, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DataEntrada, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DataSaida, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DataSaida, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DataSaida, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TipoQuarto, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TipoQuarto, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TipoQuarto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NumeroQuarto, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NumeroQuarto, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NumeroQuarto, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NumeroNoites, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.NumeroNoites, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.NumeroNoites, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Preço, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Preço, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Preço, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Observaçoes, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Observaçoes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Observaçoes, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Salvar" class="btn btn-default" />
            </div>
        </div>
    </form>
</div>
}

However, when I try to change this code from the top to this code, in order to change the format ( aesthetic reason ) give me the following error !

Code

<div class="container">
    <form class="well form-horizontal" action=" " method="post" id="contact_form">
        <div class="form-group">
            <label class="col-md-4 control-label">Nome Cliente</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                    <input name="Nome Cliente" class="form-control" value="@Model.Cliente.Nome">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">Data Entrada</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                    <input name="Data Entrada" class="form-control" value="@Model.DataEntrada">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">Data Saída</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                    <input name="Data Saída" class="form-control" value="@Model.DataSaida">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">Tipo Quarto</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-hotel"></i></span>
                    <input name="Tipo Quarto" class="form-control" value="@Model.TipoQuarto">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">NºQuarto</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-hotel"></i></span>
                    <input name="NºQuarto" class="form-control" value="@Model.NumeroQuarto">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">NºNoites</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-hotel"></i></span>
                    <input name="NºNoites" class="form-control" value="@Model.NumeroNoites">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">Preço</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-euro"></i></span>
                    <input name="Preço" class="form-control" value="@Model.Preço">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label">Observações</label>
            <div class="col-md-4 inputGroupContainer">
                <div class="input-group">
                    <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
                    <textarea class="form-control" name="Observações" value="@Model.Observaçoes"></textarea>
                </div>
            </div>
        </div>
    </form>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        <label class="col-md-4 control-label"></label>
        <div class="col-md-4">
            <button type="submit" class="btn btn-success">Editar<span class="glyphicon glyphicon-edit"></span></button>
            @Html.ActionLink("Ir para a listagem de Reservas", "Index")
        </div>
    </div>
}

By executing this give me this mistake

Imagem do Erro

Entity

namespace WebApplication.Models.BaseDados
{
    using System;
    using System.Collections.Generic;

    public partial class Reserva
    {
        public int ID_Reserva { get; set; }
        public int ID_Cliente { get; set; }
        public System.DateTime DataEntrada { get; set; }
        public Nullable<System.DateTime> DataSaida { get; set; }
        public string TipoQuarto { get; set; }
        public int NumeroQuarto { get; set; }
        public Nullable<int> NumeroNoites { get; set; }
        public Nullable<decimal> Preço { get; set; }
        public string Observaçoes { get; set; }

        public virtual Cliente Cliente { get; set; }
    }
}

3 answers

1

When a post in a action of a controle that takes an object as parameter it "takes" the data through the name properties. Probably the error when saving occurs because it is not going to the filled values. Change the names for the same property name, example

Before:

<input name="Tipo Quarto" class="form-control" value="@Model.TipoQuarto">

Afterward:

<input name="TipoQuarto" class="form-control" value="@Model.TipoQuarto">

Obs: Why not use razor, whereas the inputs are the same as those generated by him?

0

To see errors use the following code in your savaChange

try
        {
             ... o restante do codigo
            db.SaveChanges();
        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"",
                        ve.PropertyName,
                        eve.Entry.CurrentValues.GetValue<object>(ve.PropertyName),
                        ve.ErrorMessage);
                }
            }
            throw;
        }

So you can see which fields are error when validating, your error is not in cshtml but in your Cs file muitoprovavel within the entity this usually occurs when fields are null values

put a breakpoint to see the return on that code I gave you

  • I used it, but did not show me the mistakes ... I think the mistake has to do with Id_reserva

  • u debugged in the line of this code, usually do not print on the console had a similar error to that

  • you’d better post the code of your Entity(entity) instead of View

  • I made some changes to the code you gave me... now it says the error. You gave me this ---> 'The 'The Type field is required. 'Property does not exist or is not Mapped for the type 'Reserva'.'

  • Right, half way through but I need you to post your Entity here to see the field, if you’re using the Fluent api, post the Mapping too

  • the error says that the Type field is necessary but does not exist or is not being mapped in Entity Reserve, ie it is requesting a value that does not exist, very likely the error is in the relationship of its tables or in its mapping, if you have mapped this field as required you have to provide it at the time of saving the data in the database

  • I’ve just edited the question and I’ve already put the entity, what I have in it. As for the maping did not realize what you mean , I started these days in Mvc and I’m still struggling

  • 1

    the mapping that @Marcosbrinner meant is whether the Typoquarto property is configured for Dataannotation or Mapping Fluent API.

Show 3 more comments

0

Taking into account that the error was burst in the db.SaveChanges() as System.Data.Entity.Validation.DbEntityValidationException, recommend that you check the object passed in the method of Edit (Reserva reserva). But quickly looking at your cshtml modified and taking into account that you are making an edit of the data already entered, the Id for change is missing. For example in your old view displays Hidden: @Html.HiddenFor(model => model.ID_Reserva), but in your new view I did not identify any component by delivering id for modification.

OBS: A quick way to "view" this problem, would be to put a breakpoint in your method and check the parameter reserva for a better understanding of the object being altered.

  • added this <input type="Hidden" id="Id_reserva" name="Id_reserva" value="@Model.Id_reserva" /> but gave the same error ...

  • @Heftysilva, is there any way to get past what’s coming from the reserve parameter? That this generated Exception is clearly something that goes against the model implemented in your code.

  • @Danielnicodemos of a read in the comments of my answer he managed to identify the error that is in Tipoquarto, maybe help to find the solution

  • 1

    its error is The 'The Type field required. 'Property does not exist or is not Mapped for the type 'Reserve'.'

  • Then it can be one of the situations: 1 - It can be mapped as not null and sent null in the edition. 2 - Model is not mapped to the property.

  • @Marcosbrinner, this error still does not escape from the situation of the past object. It would be great if he published the model Reserva and what is being passed in this parameter as well. But the path is the same signaled, the same validation of the model to what is being passed by the parameter.

  • I’ve already published the Reserve Model,

  • @Heftysilva Check if in passing the parameter reserva the property TipoQuarto is being sent as null

  • I don’t understand how I can do that, but I believe that’s the problem !

  • @Heftysilva, put a breakpoint in the method and as soon as you call this edit method press SHIFT + F9. The Quickwatch screen will appear. In it you write the name of the parameter you passed (in this case the parameter reserva) and press enter. Inside this added, navigate to the properties of this object (in which case you will search within the parameter reserva the property TipoQuarto)

  • From now on, thanks for the help!! I just did that and in Tipoquarto appears as null

Show 6 more comments

Browser other questions tagged

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