Translate

domingo, 30 de marzo de 2014

jQuery Date Picker en español en ASP.NET MVC


Esta seria la configuración del componente datePicker para que aparezca en Español, permita escoger mes y año y con animación de scroll hacia abajo.


Esta sería la forma de usarlo en ASP.NET  MVC asociando el resultado a un campo de una tabla, y asignando dicho campo para que al editar muestre la fecha guardada.

Ojo que el dato devuelto por el datePicker es DateTime2, y no olvidar incluir en el view referencia al fichero del script definido anteriormente. A modo de ejemplo, esto sería el código de un view que usa el datePicker

jueves, 27 de marzo de 2014

ASP:NET MVC More than one model in the same view

In this case I have two models that I need to pass to the view because I need data from both classes. The first one is associated to the class UserProfile which manages the userName, userID, email, and all the data for a given user:


The second model manages the passwords for the user name. These data are stored encrypted  into a different file in order to prevent access to see the password. Both classes have Data Annotations to describe label fields,keys, required data, etc. In this link you could find info about regular expressions to validate data entered in data fields.



In my example I need  to use data from both classes(models) when a new user is being registered, that is, enter username, password and all the other data, so I need to pass both models to the same view, in which data could be entered. This is the code for the view, using data from both models:


An this is how this view is showed in the browser:


MVC allows you to pass only one model to a View, but you can use different ways to pass more than one. The way I do it, is using an auxiliary class in which you can put all the models you want to pass to the view. So this is the additional class including the two classes mentioned above:


And this is the method associated with the view, saving data for both models:


lunes, 3 de marzo de 2014

ASP.NET Setting and reading a cookie



The first method creates an instance of a Cookie for the user "agivenuser", for this example, we want to store the name, second name, and date of creation. Also we need to set the expiration date for the cookie, in this case only three days. This means that after three days without the user entering the system the cookie will be deleted.

 protected void btnSetcookie_Click(object sender, EventArgs e)
        {
            //Create an instance of a cookie.
            HttpCookie userCookie = new HttpCookie("agivenuser");


            //Store data
            userCookie["Name"] = txbxName.Text;
            userCookie["SecondName"]= txbxApellidos.Text;
            userCookie["Date"]=DateTime.Now.ToShortDateString();


            //Set expiration dates.
            userCookie.Expires = DateTime.Now.AddDays(3);


            //Store the cookie in the user machine.
            Response.Cookies.Add(userCookie);
        }




And this method is used to read the cookie for the user

protected void btnGetCookieData_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["USER"] == null)
            {
                Response.Write("USER has no cookies");
            }
            else
            {
                HttpCookie aCookie=Request.Cookies["USER"];
                Response.Write("Cookie stored :" + aCookie["Name"] + " " + aCookie["SecondName"] 

                                          +   "  " + aCookie["Date"] + " Expires :" + aCookie.Expires.ToString() );
            }
        }

sábado, 1 de marzo de 2014

C# Reflection: How to obtain fields, properties, etc.. from a Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Clases
{
    class AClassExample
    {
        private int _Id;
        private string _Name;
        private DateTime _Date;
                      
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public DateTime Date
        {
            get { return _Date; }
            set { _Date = value; }
        }

        public AClassExample(int anId, string aName, DateTime aDate)
        {
            _Id = anId;
            _Name = aName;
            _Date = aDate;
        }
    }

    class Program
    {
        public static void printObject(AClassExample aClass)
        {
            Type myObjectType = typeof(AClassExample);

            //Get Fields from object           
            System.Reflection.FieldInfo[] fieldInfo =              myObjectType.GetFields(System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public );
           //The  Binding flags allows to show the private fields

            foreach (System.Reflection.FieldInfo field in fieldInfo)
                Console.WriteLine(field.Name + ':' + field.GetValue(aClass));


            //Getting Properties from object
            System.Reflection.PropertyInfo[] propInfo = myObjectType.GetProperties();

            foreach (System.Reflection.PropertyInfo prop in propInfo)
                Console.WriteLine(prop.Name + ':' + prop.GetValue(aClass));

        }

        static void Main(string[] args)
        {
            AClassExample example = new AClassExample(2,"Un nombre",DateTime.Now);
            printObject(example);
            Console.ReadLine();
        }
    }
}