Error ASP.NET MVC

0

I have this code, working perfectly just the way I want it

@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 up to this code, in order to change the format (aesthetic motif) gives 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>
}

When running this give me this error

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; }
    }
}
    
asked by anonymous 13.04.2018 / 19:35

3 answers

0

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

Before:

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

After:

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

obs: Why not use razor , where inputs are the same as those generated by it?

    
13.04.2018 / 19:56
0

To see the 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 what fields are occurring error when validating, your error is not in cshtml but in your cs file that is very large inside the entity this usually occurs when fields are null

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

    
13.04.2018 / 20:03
0

Given that the error has popped in db.SaveChanges() as System.Data.Entity.Validation.DbEntityValidationException , I recommend that you check the object passed in the Edit ( Reserva reserva ) method. But briefly looking at your% modified% and taking into account that you are editing the already entered data, the Id is missing for change. For example in your old view it shows the hidden: cshtml , but in your new view I did not identify any component delivering the id for modification.

A quick way to "visualize" this problem would be to place a breakpoint in your method and check the @Html.HiddenFor(model => model.ID_Reserva) parameter for a better understanding of the object being changed.

>     

13.04.2018 / 20:07