Link to home
Start Free TrialLog in
Avatar of Steven
StevenFlag for United States of America

asked on

Unable to cast object of type 'System.Char' to type 'System.String'. MVC asp.net

Good afternoon,
I'm having an issue here, I was assigned a project, I decided to use it to learn MVC as I was doing it. so what I'm saying is that I'm new to asp.net MVC.  I wont post my controller since its not been fire, I get the error before it fires my [httppost]
this is the error I'm getting: Unable to cast object of type 'System.Char' to type 'System.String'.
here is the models

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

namespace BiciPublica.Models
{
    public class Usuario
    {
        public int Id { get; set; }
        
        [Required]
        [StringLength(80)]
        public string Nombres { get; set; }

        [Required]
        [StringLength(40)]
        [Display(Name ="Primer apellido")]
        public string PrimerApellido { get; set; }

        [Required]
        [StringLength(40)]
        [Display(Name = "Segundo apellido")]
        public string SegundoApellido { get; set; }

        [Required]
        [StringLength(30)]
        public string Cedula { get; set; }

        [Required]
        [Display(Name = "Fecha nacimiento ")]
        public DateTime FechaNacimiento { get; set; }

        [Required]
        [StringLength(5)]
        public string Rh { get; set; }

        [Required]
        [StringLength(10)]
        public char Genero { get; set; } 

        [Required]
        [StringLength(200)]
        [Display(Name = "Dirección")]
        public string Direccion { get; set; }

        [Required]
        [StringLength(200)]
        [Display(Name = "Barrio")]
        public string Barrio { get; set; }

                
        public Municipio Municipio { get; set; }

        [Display(Name = "Municipio")]
        public int MunicipioId { get; set; }
        

        [Display(Name ="Telefono fijo")]
        public string TelefonoFijo { get; set; }

        [Required]
        [StringLength(30)]
        public string Celular { get; set; }

        [Required]
        [StringLength(254)]
        public string Correo { get; set; }

        public string Foto { get; set; }

        public string ImagenCC { get; set; }

        public string NumeroIdServicio { get; set; }

        [Required]
        public bool ResidenteAMB { get; set; }
    
        [Required]
        public DateTime FechaInicioRegistro { get; set; }

        [Required]
        public Boolean Validado { get; set; }
                
        public DateTime? FechaFinalizaRegistro { get; set; }

        public int Pin { get; set; }



    }
}

Open in new window


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

namespace BiciPublica.Models
{
    public class Municipio
    {
        public int Id { get; set; }

        [Required]
        [StringLength(150)]
        public string Nombre { get; set; }

        public Departamento Departamento { get; set; }

        public Int16 DepartamentoId { get; set; }
    }
}

Open in new window

now the view model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BiciPublica.Models;

namespace BiciPublica.ViewModels
{
    public class NuevoUsuarioViewModel
    {
        public IEnumerable<BiciPublica.Models.Municipio> Municipios { get; set; }
        public Usuario Usuario { get; set; }
    }
}

Open in new window


here is the view
@model BiciPublica.ViewModels.NuevoUsuarioViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="header-page-bici">
    <div class="container">
        <h1>Inscripción </h1>
        <p>Ingresa tu información para poder iniciar a utilizar los servicios de Ponte BICI.</p>
    </div>
</div>
@using (Html.BeginForm("Crear", "Usuario"))
{
    @Html.AntiForgeryToken()

    <div class="container">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Nombres)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Nombres)
                          .Name("Nombre")
                          .Value("William")
                              .HtmlAttributes(new { placeholder = "Nombres", required = "required", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.PrimerApellido)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.PrimerApellido)
                              .Name("PrimerApellido")
                              .Value("Wallace")
                              .HtmlAttributes(new { placeholder = "Primer Apellido", required = "required", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.SegundoApellido)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.SegundoApellido)
                              .Name("SegundoApellido")
                              .Value("Gil")
                              .HtmlAttributes(new { placeholder = "Segundo Apellido", required = "required", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Cedula)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Cedula)
                              .Name("Cedula")
                              .Value("1325854")
                              .HtmlAttributes(new { placeholder = "No signos de puntuacion", required = "required", pattern = "\\d+", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.FechaNacimiento)
   
            @(Html.Kendo().DatePickerFor(m => m.Usuario.FechaNacimiento)
              .Name("datepicker")
              .Value("1/07/1965")
              .HtmlAttributes(new { style = "width: 100%", required = "required", })                )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Rh)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Rh)
                              .Name("RH")
                               .Value("O+")
                              .HtmlAttributes(new { placeholder = "ej. O+", required = "required", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>

        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Direccion)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Direccion)
                              .Name("Direccion")
                               .Value("cll 12 # 5 -315")
                              .HtmlAttributes(new { placeholder = "", required = "required", validationmessage = "Ingrese {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Barrio)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Barrio)
                              .Name("Barrio")
                               .Value("1/07/1965")
                              .HtmlAttributes(new { placeholder = "Nombre del Barrio", required = "required", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        @*<div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.MunicipioId)
            @(Html.Kendo().DropDownListFor(m => m.Usuario.MunicipioId)
                                  .BindTo(new SelectList(Model.Municipios, "Id", "Nombre"))
                                  .HtmlAttributes(new { required = "required", style = "width: 100%" })

            )
        </div>*@
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.TelefonoFijo)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.TelefonoFijo)
                              .Name("TelefonoFijo")
                               .Value("6553265")
                              .HtmlAttributes(new { placeholder = "ej. 6356556", validationmessage = "Enter {0}", style = "width:100%" })
            )
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Celular)
            @(Html.Kendo().TextBoxFor(m => m.Usuario.Celular)
                  .Name("Celuar")
                   .Value("3172266756")
                  .HtmlAttributes(new { required = "required", placeholder = "Ingrese numero telefonico", pattern = "\\d{10}", type = "tel", style = "width:100%;" })                )
            @Html.ValidationMessageFor(model => model.Usuario.Celular, "", new { @class = "text-danger" })
        </div>
        <div class="form-group col-md-4">
            @Html.LabelFor(model => model.Usuario.Correo)<br />
            @(Html.Kendo().TextBox()
                              .Name("email")
                               .Value("steve_724@hotmail.com")
                              .HtmlAttributes(new { placeholder = "ej: minombre@ejemplo.net", required = "required", type = "email", data_email_msg = "Formato de correo incorrecto.", style = "width: 100%;" })
            )
        </div>
        <div class="checkbox col-md-4" style="padding-top:20px;">
            <label>
                @*@Html.CheckBoxFor(model => model.Usuario.ResidenteAMB)Residente Area Metropolitana Bucaramanga?*@
            </label>
            @Html.ValidationMessageFor(model => model.Usuario.ResidenteAMB, "", new { @class = "text-danger" })
        </div>
        <div class="row">
            <div class="checkbox col-md-12" style="padding-top:20px;">
                <label>
                    @*@Html.CheckBoxFor(model => model.Usuario.ResidenteAMB)Confirmo que he leído y acepto el contrato de uso PonteBICI*@
                </label>
                @*@Html.ValidationMessageFor(model => model.Usuario.ResidenteAMB, "", new { @class = "text-danger" })*@
            </div><br />

            @*Confirmo que he leído y acepto el contrato de uso PonteBICI.
                Confirmo que tengo tarjeta Metrolinea
                Confirmo que tengo cobertura de salud*@
        </div>
        <div class="form-group col-md-12 text-center">
            <button type="submit"  class="btn btn-default" >Inscribirse</button>
        </div>
    </div>
}

Open in new window

I apologize in advance if its something simple.  thanks.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

run the code in the VS debugger and it will show you the line of the code that is generating the error and in which code file
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steven

ASKER

Awesome, I knew it had to be something simple, Im really sorry. "no char no conversion no error" this will be my status for a while, lol. I really appreciate your help. now I got a new set of errors but I'll try to fix them myself first.  Thanks once more.