Problem datetime value null

0

It turns out that when I run my view, I get this error

Now, my controller is this:

        public ActionResult contact(string txtNombreArchivo, string txtCantidadTarjetas, string txtNombreAfiliado, string txtBIN, DateTime dtpFechaVigencia, string txtTextoMonto, string txtMonto, string btnSubmit)
    {
        BC.Funciones funciones = new Funciones();
        DAC.Axon bd = new DAC.Axon();
            try
            {

                if (!Directory.Exists(@"Respaldo"))
                {
                    Directory.CreateDirectory(@"Respaldo");
                }

                funciones.EscribirCSV("Numero Afiliado;Cuenta;Clave;Vigencia;Nombre afiliado;Texto;Track1 Banda;Track2 Banda;EAN13;BIN", int.Parse(txtCantidadTarjetas.ToString()), txtNombreArchivo.ToString());
                Random rnd = new Random();

                int consecutivo = 1;
                int random9 = rnd.Next(100000000, 999999999);

                int rowcount = int.Parse(txtCantidadTarjetas.ToString());



                TarjetaTicket[] tarjetaTickets = new TarjetaTicket[int.Parse(txtCantidadTarjetas.ToString())];
                for (int i = 0; i <= rowcount - 1; i++)
                {
                    BC.TarjetaTicket tarjetaTicket = new TarjetaTicket();
                    int nro1 = rnd.Next(10000000, 99999999);
                    int nro2 = rnd.Next(10000000, 99999999);
                    int clave = rnd.Next(1000, 9999);
                    int quince1 = rnd.Next(10000000, 99999999);
                    int quince2 = rnd.Next(1000000, 9999999);

                    string Track1 = "B" + nro1 + nro2 + "^    /                     ^" + random9.ToString();
                    string Track2 = nro1.ToString() + nro2.ToString() + "=" + quince1 + quince2;
                    funciones.EscribirCSV(consecutivo.ToString("0000") + ";" + nro1.ToString() + nro2.ToString() + ";" + clave + ";" + dtpFechaVigencia.Date.ToString().Substring(0, 10) +
                        ";" + txtNombreAfiliado.ToString() + ";" + txtTextoMonto.ToString() + ";" + Track1 + ";" + Track2 + ";" + nro1 + nro2.ToString().Substring(0, 4) + ";" + txtBIN.ToString(), int.Parse(txtCantidadTarjetas.ToString()), txtNombreArchivo.ToString());
                    consecutivo++;


                    tarjetaTicket.NumeroTarjeta = nro1.ToString() + nro2.ToString();
                    tarjetaTicket.FechaCreacionTarjeta = DateTime.Now;
                    tarjetaTicket.FechaVigenciaTarjeta = dtpFechaVigencia;
                    tarjetaTicket.MontoTarjeta = txtMonto.ToString();
                    tarjetaTicket.HashOriginal = rnd.Next(100000, 999999).ToString();

                    tarjetaTickets[i] = tarjetaTicket;
                }


                BC.Ticket ticket = new Ticket();
                ticket.NombreTicket = txtNombreArchivo.ToString();
                ticket.Bin = txtBIN.ToString();
                ticket.TotalTarjetas = int.Parse(txtCantidadTarjetas.ToString());
                ticket.FechaCreacionTicket = DateTime.Now;
                ticket.NombreAfiliado = txtNombreAfiliado.ToString();


            }

            catch (Exception ron)
            {
                Exception ex;
                //MessageBox.Show(ron.Message);
            }

            return View(funciones);
        }




    private void EscribirCSV(string mensaje, int cantidad, string txtNombreArchivo)

    {
        //Método utilizado para probar
        //NO SE SI ESTA BIEN
        System.IO.StreamWriter streamWriter = new StreamWriter(@"Respaldo" + "\" + "F28112018_123456_T_789456_" + txtNombreArchivo.ToString() + "_" + cantidad + "_12345678" + ".csv");
        streamWriter.WriteLine(mensaje);
        streamWriter.Close();
    }

this is my vi

<body>
</body>
<div class="jumbotron">
    <form action="~/Home/contact" />
    <form method="post">
        <div>Nombre archivo</div>
        <div><input type="text" name="txtNombreArchivo" class="form-control" /></div>
        <div>Cantidad tarjetas</div>
        <div><input type="text" name="txtCantidadTarjetas" class="form-control"/></div>
        <div>nombre Afiliado</div>
        <div><input type="text" name="txtNombreAfiliado"class="form-control" /></div>
        <div>BIN</div>
        <div><input type="text" name="txtBIN"class="form-control" /></div>
        <div>fecha vigencia</div>
        <div><input type="date" name="dtpFechaVigencia"class="form-control"/></div>
        <div>monto</div>
        <div><input type="text" name="txtMonto" class="form-control"/></div>
        <div>textmonto</div>
        <div><input type="text" name="txtTextoMonto"class="form-control" /></div>
        <input name="btnSubmit" type="submit" value="agregar" />
    </form>
</div>

sta

thanks in advance

    
asked by KonnAN 04.12.2018 в 00:11
source

2 answers

2

The first problem I see is that you define two tags form when it should be

<form action="~/Home/contact" method="post" >

also the first form you are closing with the / and in the second you only define the method

On the other hand you should not put txt or dpt, etc in the controls, that's very classic asp.net, it is not used in asp.net mvc.

The btnSubmit does not have to define it, the button is not defined as part of the parameters, it only performs the action and nothing else

Also when you have many parameters you should define a model in a class and not each parameter

public class ContactModel
{
    public string txtNombreArchivo {get;set;}
    public string txtCantidadTarjetas {get;set;}
    public string txtNombreAfiliado {get;set;}
    public string txtBIN {get;set;}
    public DateTime dtpFechaVigencia {get;set;}
    public string txtTextoMonto {get;set;}
    public string txtMonto {get;set;}
}

in the action you would use

public ActionResult contact(ContactModel model)
{
   //resto codigo
}
    
answered by 04.12.2018 в 02:44
0

for that case you can add a validation. pEjm:

if(dtpFechaVigencia != null)
{
  tarjetaTicket.FechaVigenciaTarjeta = dtpFechaVigencia;
}

If this value is null, you avoid that error

    
answered by 04.12.2018 в 14:42