Compiling Error - Trying to insert a Client in a Asp .Net MVC Client_Index

  • Thread starter Thread starter F.oliveirarocha
  • Start date Start date
F

F.oliveirarocha

Guest
Hi folks, This is my first projet in Asp .Net MVC, I'm starting a Client File, Insert and then consult.

I'm getting an Error trying to insert a new client. The Table Client -> "ClientID" is Identity.

I can't figure out where is the Error, after 8 hours at the screen. Please help me out.

The error ocurrs when I get the view after insert the client. I don't know which references I have to use it and how

Thanks in advance for your help.

Here follows the Error Message:

Erro de Servidor no Aplicativo '/'.


Erro de Compilação

Descrição: Erro ao compilar um recurso necessário para atender esta solicitação. Examine os detalhes específicos do erro e modifique o código fonte apropriadamente. Mensagem de Erro do Compilador: CS1061: 'IEnumerable<ClientViewModel>' does not contain a definition for 'ClienteID' and no extension method 'ClienteID' accepting a first argument of type 'IEnumerable<ClientViewModel>' could be found (are you missing a using directive or an assembly reference?) Erro de Origem:



Linha 14: <tr>

Linha 15: <th>

Linha 16: @Html.DisplayNameFor(model => model.ClienteID)

Linha 17: </th>

Linha 18: <th>


Arquivo de Origem: D:\AUTOCARWEB\AutocarWeb\AutocarWeb\Views\Client\Index.cshtml Linha: 16


Mostrar Saída Detalhada do Compilador:


Mostrar Origem de Compilação Completa:



Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.8.3928.0

Here I will insert the code: This is the ViewModel.

@model AutocarWeb.ViewModels.ClientViewModel

@{
ViewBag.Title = "Insert";
}

<h2>Client File</h2>


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

<div class="form-horizontal">
<h4>ClientViewModel</h4>
<hr />


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

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

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

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

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

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

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

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

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

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

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

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

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}


Here is where the error occurs: Index (View -> Client --> Index - here is where the programm breaks. It should display all Clients after Insert.

@using AutocarWeb.ViewModels;
@model IEnumerable<AutocarWeb.ViewModels.ClientViewModel>

@{
ViewBag.Title = "Clients";
}

<h2>Clients</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ClienteID)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.County) // not compiling
</th>
<th>
@Html.DisplayNameFor(model => model.celular_phone)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClienteID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name) // not compiling
</td>
<td>
@Html.DisplayFor(modelItem => item.County)
</td>
<td>
@Html.DisplayFor(modelItem => item.celular_phone)
</td>
<td>
@Html.ActionLink("Editar", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Detalhes", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Excluir", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}

</table>


Here goes the Client Controller: Note that the ClientID is Identity at the table. So, I don't insert it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;
using AutocarWebConnection.Interface;
using System.Domain;
using System.Domain.Business;
using System.Domain.Interfaces;
using System.Domain.Repositories;
using AutocarWeb.ViewModels;

namespace AutocarWeb.Controllers
{
public class ClientController : Controller
{
private ClientBusiness clientBusiness = new ClientBusiness();
// GET: Client
public ActionResult Index()
{
var clients = clientBusiness.ListallClients();
return View(clients); // this view is not working. It breaks. Should list all clients.
}

[HttpGet]
public ActionResult Insert()
{
return View();
}

[HttpPost]
public ActionResult Insert(ClientViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}

var client = new Client
{
Name = model.Name,
Address = model.Address,
City = model.City,
County = model.County,
State = model.State,
Zip_Code = model.Zip_Code,
CPF = model.CPF,
Idnumber = model.Idnumber,
email = model.email,
celular_phone = model.celular_phone,
home_phone = model.home_phone
};

clientBusiness.Save(client); // Clients are being saved to the table.
return RedirectToAction("Index");
}

}


}

Continue reading...
 
Back
Top