Error loading form data

0

I'm making a form that loads data and shows it in a view with MVC.

Customer.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



namespace Formulario2.Models

{

    public class Customer

    {

        public string Code { get; set; }

        public string Name { get; set; }

        public int Amount { get; set; }

    }

}

CustomerController.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Formulario2.Models;



namespace Formulario2.Controllers

{

    public class CustomerController : Controller

    {

        // GET: Customer

        public ViewResult DisplayCustomer(CustomerController objCustomer)

        {

            return View(objCustomer);
        }

        public ActionResult FillCustomer()

        {



            return View("FillCustomer");

        }

    }

}

FillCustomer.cshtml

@model Formulario2.Models.Customer

@{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>FillCustomer</title>

</head>

<body>

    <div>

        @using (Html.BeginForm("DisplayCustomer", "Customer", FormMethod.Post))

        {

            <p>

                @Html.LabelFor(model=>model.Code,"Código")

                @Html.EditorFor(model => model.Code)

            </p>

            <p>

                @Html.LabelFor(model => model.Name, "Nombre")

                @Html.EditorFor(model => model.Name)

            </p>

            <p>

                @Html.LabelFor(model => model.Amount, "Cantidad")

                @Html.EditorFor(model => model.Amount)

            </p>

            <input type="submit" value="Enviar" />



        }



    </div>

</body>

</html>

DisplayCustomer.cshtml

@Model Formulario2.Models.Customer
  @{

    Layout = null;

}



<!DOCTYPE html>



<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>DisplayCustomer</title>

</head>

<body>

    <div>

        El codigo del cliente es @Model.Code<br />

        El codigo del cliente es @Model.Name<br />

        @if (Model.Amount > 100)

        {

            <p>Este es el cliente privilegiado</p>

        }



        else

        {

            <p>Este es el cliente normal</p>

        }





    </div>

</body>

</html>

When I fill in the form data, an exception occurs on line 15 of DisplayCustomer.cshtml

El código de usuario no controló Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

  HResult=-2146233088

  Message='Formulario2.Controllers.CustomerController' no contiene una definición para 'Code'.

  Source=Anonymously Hosted DynamicMethods Assembly

  StackTrace:

       en CallSite.Target(Closure , CallSite , Object )

       en System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

       en ASP._Page_Views_Customer_DisplayCustomer_cshtml.Execute() en C:\Users\Usuario\Documents\Visual Studio 2015\Projects\practicando\ASP mvc\Formulario2\Views\Customer\DisplayCustomer.cshtml:línea 15

       en System.Web.WebPages.WebPageBase.ExecutePageHierarchy()

       en System.Web.Mvc.WebViewPage.ExecutePageHierarchy()

       en System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)

       en System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)

       en System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)

       en System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)

       en System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)

       en System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList'1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

       en System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList'1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

  InnerException:

If I put a model with a lower case, it tells me that Code and Name do not exist in the current context. The same with the if model.

Preposing @model Formulario2.Models.Customer, it is not solved.

What can it be?

    
asked by Andrés Oporto 21.04.2017 в 21:36
source

3 answers

0

Your Action Method DisplayCustomer must receive an object of the type of your model, that is, of the 'Customer' type, plus it must be decorated to receive an http post.

    [HttpPost]
    public ViewResult DisplayCustomer(Customer objCustomer)
    {
        return View(objCustomer);
    }
    
answered by 21.04.2017 / 23:39
source
0

The error is in the controller. You are seeing a controller:

    public ViewResult DisplayCustomer(CustomerController objCustomer)
    {

        return View(objCustomer);//Aquí esta el error.
         //Estas pasando un tipo CustomerController cuando deberias pasar un Customer
    }
    
answered by 21.04.2017 в 22:14
0

is not @Model Formulario2.Models.Customer is @model Formulario2.Models.Customer

with a lower case, here a similar problem, but it's in English.

    
answered by 21.04.2017 в 23:19