MVC - Return View (Model) when I make a POST

0

I have the following problem.

My Controller has 2 Functions. This:

<AllowAnonymous, HttpGet>
    Function Register(urlBack As String) As ActionResult
        Try
            Dim DBIvas As New BLIva()
            Dim DBProvincias As New BLProvincia()

            ViewBag.CondicionesIva = DBIvas.TraeTodos(Nothing, Nothing, "IdIva")
            ViewBag.Provincias = DBProvincias.TraeTodos(Nothing, "Activo=1 AND IdPais=1", "Descripcion")
            ViewBag.urlBack = urlBack

            If Request.IsAuthenticated Then 'Usuario LOGGEADO
                Dim Model As RegisterViewModel = ModelToModel(Usuario, New RegisterViewModel)

                Dim DBLocalidades As New BLLocalidad
                ViewBag.Localidades = DBLocalidades.TraeTodos(Nothing, "Activo=1 AND IdProvincia=" & Model.IdProvincia, "Descripcion")

                Dim DBBarrios As New BLBarrio
                ViewBag.Barrios = DBBarrios.TraeTodos(Nothing, "Activo=1 AND IdLocalidad=" & Model.IdLocalidad, "Descripcion")

                Return View(Model)
            End If

            Return View()
        Catch ex As Exception
            Throw ex
        End Try
    End Function

and this one:

<HttpPost, AllowAnonymous, ValidateAntiForgeryToken>
        Function Register(Model As RegisterViewModel, urlBack As String) As ActionResult
            If ModelState.IsValid Then
                Dim Cliente As mdlCliente = ModelToModel(Model, New mdlCliente)
                If DBClientes.RegistrarUsuarioWeb(Cliente) Then
                    Return Redirect(urlBack)
                Else
                    'Agrego el ERROR.
                    ModelState.AddModelError("ID_ERROR", "El proceso fallo, intentelo de nuevo.")
                End If
            Else
                'Agrego el ERROR.
                Dim FirstError As KeyValuePair(Of String, ModelState) = ModelState.FirstOrDefault(Function(x) x.Value.Errors.Count > 0)
                ModelState.AddModelError("ID_ERROR", FirstError.Value.Errors.First().ErrorMessage)
            End If

            'Otros datos necesarios para la VIEW()
            Dim DBIvas As New BLIva()
            Dim DBProvincias As New BLProvincia()

            ViewBag.CondicionesIva = DBIvas.TraeTodos(Nothing, Nothing, "IdIva")
            ViewBag.Provincias = DBProvincias.TraeTodos(Nothing, "Activo=1 AND IdPais=1", "Descripcion")
            ViewBag.urlBack = urlBack

            If Model.IdProvincia <> Nothing Then
                Dim DBLocalidades As New BLLocalidad
                ViewBag.Localidades = DBLocalidades.TraeTodos(Nothing, "Activo=1 AND IdProvincia=" & Model.IdProvincia, "Descripcion")

                If Model.IdLocalidad <> Nothing Then
                    Dim DBBarrios As New BLBarrio
                    ViewBag.Barrios = DBBarrios.TraeTodos(Nothing, "Activo=1 AND IdLocalidad=" & Model.IdLocalidad, "Descripcion")
                End If
            End If

            Return View(Model)
        End Function

When the ModelState.IsValid returns False or fails the RegisterUserWeb () function

The httpPost function returns View (Model).

This is my VIEW

<div id="content">
<div class="container">
    <div class="col-md-12">
        <ul class="breadcrumb">
            <li>
                <a href="/Home/Index">Inicio</a>
            </li>
            <li>Registrarse / Ingresar</li>
        </ul>
    </div>
    <div class="col-md-12">
        <div id="login-partial" class="box">
            @Html.Action("_Login", "Account", New With {.urlBack = urlBack})                
        </div>
    </div>
    <div Class="col-md-12">
        <div Class="box">
            @if Request.IsAuthenticated Then

            Else
                @<p Class="lead">¿Aún no es un usuario registrado?</p>
                @<h1> Registrarse</h1>
                @<p> Registrate y accede a increibles descuentos, promociones y productos! El proceso es gratuito y no te llevara mas que unos minutos!</p>
                @<hr />
            End If

            @Using (Html.BeginForm("Register", "Account", FormMethod.Post, New With {.id = "formRegister", .class = IIf(Request.IsAuthenticated, "disabled", "")}))
                @Html.AntiForgeryToken()
                @Html.Hidden("urlBack", urlBack)

                @Html.ValidationSummary(True, "", New With {.class = "text-danger"})
                @Html.ValidationMessage("ID_ERROR", New With {.class = "text-danger"})

                @<div Class="row">
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.Mail)
                        @Html.TextBoxFor(Function(x) x.Mail, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Mail, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.ClaveWeb)
                        @Html.TextBoxFor(Function(x) x.ClaveWeb, New With {.class = "form-control", .type = "password"})
                        @Html.ValidationMessageFor(Function(x) x.ClaveWeb, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-9">
                        @Html.LabelFor(Function(x) x.RazonSocial, "Razon Social/Apellido y Nombre")
                        @Html.TextBoxFor(Function(x) x.RazonSocial, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.RazonSocial, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-3">
                        @Html.LabelFor(Function(x) x.Telefono, "Telefono/Celular")
                        @Html.TextBoxFor(Function(x) x.Telefono, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Telefono, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-4 col-sm-6">
                        @Html.LabelFor(Function(x) x.IdIva, "Cond. de IVA")
                        @Html.DropDownListFor(Function(x) x.IdIva, New SelectList(CondicionesIva, "IdIva", "Descripcion"), New With {.id = "cmbIva", .class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.IdIva, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-4 col-sm-6">
                        @Html.LabelFor(Function(x) x.CUIT)
                        @Html.TextBoxFor(Function(x) x.CUIT, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.CUIT, Nothing, New With {.class = "text-danger"})
                    </div>
                </div>
                @<hr />
                @<div Class="row">
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.IdProvincia, "Provincia")
                        @Html.DropDownListFor(Function(x) x.IdProvincia, New SelectList(Provincias, "IdProvincia", "Descripcion"), New With {.id = "cmbProvincia", .class = "form-control", .onchange = "LoadLocalidades();"})
                        @Html.ValidationMessageFor(Function(x) x.IdProvincia, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.IdLocalidad, "Localidad")
                        @Html.DropDownListFor(Function(x) x.IdLocalidad, New SelectList(Localidades, "IdLocalidad", "Descripcion"), New With {.id = "cmbLocalidad", .class = "form-control", .disabled = "", .onchange = "LoadBarrios();"})
                        @Html.ValidationMessageFor(Function(x) x.IdLocalidad, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.IdBarrio, "Barrio")
                        @Html.DropDownListFor(Function(x) x.IdBarrio, New SelectList(Barrios, "IdBarrio", "Descripcion"), New With {.id = "cmbBarrio", .class = "form-control", .disabled = ""})
                        @Html.ValidationMessageFor(Function(x) x.IdBarrio, Nothing, New With {.class = "text-danger"})
                    </div>
                </div>
                @<div class="row">
                    <div Class="form-group col-md-6">
                        @Html.LabelFor(Function(x) x.Calle)
                        @Html.TextBoxFor(Function(x) x.Calle, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Calle, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-2 col-sm-3 col-xs-6">
                        @Html.LabelFor(Function(x) x.Nro)
                        @Html.TextBoxFor(Function(x) x.Nro, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Nro, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-1 col-sm-3 col-xs-6">
                        @Html.LabelFor(Function(x) x.Piso)
                        @Html.TextBoxFor(Function(x) x.Piso, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Piso, Nothing, New With {.class = "text-danger"})
                    </div>
                    <div Class="form-group col-md-1 col-sm-3 col-xs-6">
                         @Html.LabelFor(Function(x) x.Dpto)
                         @Html.TextBoxFor(Function(x) x.Dpto, New With {.class = "form-control"})
                         @Html.ValidationMessageFor(Function(x) x.Dpto, Nothing, New With {.class = "text-danger"})
                     </div>
                    <div Class="form-group col-md-2 col-sm-3 col-xs-6">
                        @Html.LabelFor(Function(x) x.Cp)
                        @Html.TextBoxFor(Function(x) x.Cp, New With {.class = "form-control"})
                        @Html.ValidationMessageFor(Function(x) x.Cp, Nothing, New With {.class = "text-danger"})
                    </div>
                </div>
                @<hr />
                @<div Class="text-center">
                    @If Request.IsAuthenticated Then
                        @<Button type="submit" Class="btn btn-primary"><i Class="fa fa-user-md"></i>Actualizar</Button>
                    Else
                        @<Button type="submit" Class="btn btn-primary"><i Class="fa fa-user-md"></i>Registrarse</Button>
                    End If
                </div>
            End Using                    
            <hr />
            <p Class="text-muted">Si tienes alguna duda o pregunta, por favor, <a href="/Home/Contact">contactenos</a>, nuestros vendedores estan para atenderlo!</p>
        </div>
    </div>
</div>
<!-- /.container -->

When it finishes assembling the view, instead of drawing it, it shows me the source code as if it were the Web page.

I do not know if I explain myself, or I put an Image.

    
asked by Lucas 29.11.2016 в 22:30
source

1 answer

0

If the ModelState fails I do not see so you define the line

 Dim FirstError As KeyValuePair(Of String, ModelState) = ModelState.FirstOrDefault(Function(x) x.Value.Errors.Count > 0)

that and assign the ModelState.AddModelError() are more, you just have to circulate the execution to the return View(Model) without anything else to touch.

In the view you define a Html.ValidationSummary() with that it reaches to show you the messages by which the validation of the model failed. You could see to remove the parameter true (or specify a false) so that it lists all the errors.

If the RegistrarUsuarioWeb() method fails you will have an Exception but I do not see any try..catch that controls it, so you will have the asp.net mvc error page.

    
answered by 20.12.2016 в 05:15