Estoy haciendo un formulario que carga datos y lo muestra en una vista con 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>
Cuando lleno los datos del formulario, me salta una excepcion en la línea 15 de 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:
Si le pongo model con minúscula, me dice que Code
y Name
no existen en el contexto actual. Lo mismo con el model del if.
Anteponiendo a @model Formulario2.Models.Customer, no se soluciona.
Que puede ser?
public ViewResult DisplayCustomer
y nopublic ActionResult DisplayCustomer
?