Wednesday, June 6, 2012

Validation for MVC ASP.net Razor view

I got stuck with creating the validation for the MVC project and I couldn't find many resources.

There are three steps needed to implement validation in the MVC project:
1. Attribute on the variables
2. HTTP helpers Validation summery and messages on the view
3. Javascript  and Jquery libraries

Here is how you implement the validation
1.Attribute on the variables
They are placed on top of the variables stating the limitations on the variables (e.g. has to be a number, too long or short, can't be null..etc).
Here is an example that I used for a variable that can not be over 1024 and is also required.
        [Required(ErrorMessage="Value is Required")]
        [Display(Name="DialogueSubject")]
        [StringLength(maximumLength: 1024, ErrorMessage = "too long")]
        public string DialogueSubject { get; set; }

make sure you include using System.ComponentModel.DataAnnotations;

2.HTTP helpers Validation summery and messages on the view
These lines would fire up once a validation error shows up.
At the top of the form I placed the following
    @Html.ValidationSummary(true, "Error. See the notification below")
and after each one of the textboxes I placed this line. It would get the error from the attribute and tell the user which rule was violated.
    @Html.ValidationMessageFor(m => m.DialogueSubject)

3.Javascript  and Jquery libraries
And this was the part that I was stuck on. I didn't include the Jquery libraries. These are the 2 libraries that need to be included:
    jquery.validate.unobtrusive.min.js
    jquery.validate.min.js

This is what the final result would look like:

And that is it! happy validation

I need to blog more

I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more I need to blog more

It has been a while now. I need to clean up all of the drafts I made.
Saturday, January 21, 2012

College for the Non-Traditional Students and it's benefits

I will try to rush this write up as I feel like there is much to discuss and edit. and..GO

I was listening to NPR on my way home from work and they were talking about bringing back college drop outs to the classes. The part that interested me was when they talking about how the current college model does not cater to the current college student now. Many college students commute or  work while attending college unlike what universities (and societies) expects the average student to be (living in the dorm and having no other responsibilities aside from school work). Many of these (new aged) students will have to choose between work or school. On of the options will put food on the table so it is easy to drop out of school. I am hoping that this will change in the future with the introduction of the internet and online classes. Here are my reason on why non traditional students will can have a better education:

1. An early work force experience.
Sure, There is no trade of for the crazy social life in school with all of its highs and lows. But there is no trade off for the work force experience either. Having a job or two on the resume will mean a lot especially if the job is related to your dream future job that you are applying for.

2. being your own teacher.
Online classes demand a lot of self-taught skills. It could feel like cheating sometimes by attending a class and trying to find what the teacher assumes is going to be on the test over learning the actual material. I knew students who did not read the book and never heard of a UML diagram before even that it was on the 2nd chapter in a book for a 1st year Computer Science class. Teachers can not go over everything in the 45 min class time so they will only try to pick what they want the student to pass knowing.

3. Time management skills.
 If you work and attend school at the same time, then you will force yourself to get the best out of your study time and develop good study skills.



 The model of college education is outdated and it will be long before it become effective to all students. But I also think that those who are not in the target could benefit a lot more if they can survive their way through.


--
 I just hope that it helps some internet surfer looking for opinions.



Sunday, November 13, 2011

Change a textView field after creating an Activity

Problem:
I could not remember how to change the textView field after the start of the program. I got an error that was caused by the int to string conversion. I finally got my code to work and I decided to share it.
I added a button to make the application more interactive


Code:

public class Main extends Activity
{

Button add;
TextView tv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        add = (Button) findViewById(R.id.button1);
        tv = (TextView) this.findViewById(R.id.TextView01);
     
        tv.setText("0"); //the starting value
     
        add.setOnClickListener(new View.OnClickListener()
        {
        int counter = 0;
        String s = "";
       
@Override
public void onClick(View v)
{
counter++;
s =  Integer.toString(counter); //change to string
tv.setText(s); //the text will be changed here.
}
});
}
====

I am sure that I ran into a better method of adding text to the TextView. I just wanted to publish this here as it might help others getting something running. If you can see any areas where I can refine my code then please post it in the comments.
Monday, November 7, 2011

Interview Questions by Chris Foley - Starcraft 2

A teammate in my UNCC SC2 team (and a friend of mine) wanted some help with his paper. I liked the questions and I think that the answers would make a good blog post.

Starcraft Questions
What is Starcraft to you?
A game. A very good one.

What got you into Starcraft?
The pro scene. I always enjoyed video games but I had never looked at videogames as competitive until I found out youtube videos of tournaments played in Korea and WCG. I picked up the game afterward and the more I tried to improve myself (by looking strategies , practicing with mentors and applying stratagies I watched others do). The better I got in the game the more I  enjoyed it.

If you could have time to truly dedicate to Starcraft would you attempt and try to go pro with it?
Not really.. I have a huge respect for the proscene but I don't think that I would enjoy it much.

Do you see yourself playing starcraft for as long as possible?
Not starcraft, But I will surley continue playing videogames forever as long as I can find the games that would let me practice those skills

What is the most difficult part of starcraft for you?
Improve my analytic skills. I tend to get frustrated at games that I lose without knowing the reason why I lost or what should I have done better.

What motivates your to become better at starcraft?
I think a lot of the concepts that I learn from SC can be applied in real life. I see a lot of concept in my real life as game concepts. An example would be time management and how spending more effort on one part would have to be sacrificed on the other side.
Also I realized that I get the same feeling while playing as the one that I get when I am taking a test (a headache from the lack of oxygen and long period concetrating).
There is a lot of pressure when I play and I become extreimly tired. I had felt my heart beats many times and I could not sleep after games because of the adrilanin in my body. Knowing that I can become better at those situations by playing more "games" can only make me happier.


Social Questions
What is your reaction to people who call you a nerd?
I have always liked it and I embrace it more now. I know that it can be looked at negetivily, but I have always enjoyed who I am and I love it when I find other nerds to hang out with. I think that we should all be passionate about something and become nerds for what we love.


Do you have a girlfriend? If not is there any particular reason why not that you care to share?
lolno because of video games!. Ok not really but I know that is the answer that you wanted.

I am from a different culture (I moved to the US 7 years ago) and I don't have a lot in common with most of the people that I meet. It is hard enough for me to make friends due to my narrow interest (but I try!). I see relationships as a higher level of friendship so that narrow it even more. The pool is very small and to be honest, I enjoy the freedom now.

Can I just go and find a random girlfriend? of course, but I do not want to watch a crappy movie and eat dinner somewhere every weekend. I guess there are things I need to scarafice.

Do you consider yourself to have a social life outside of gaming and starcraft?
Yes. I go to clubs whenever they are having any Electronic DJs and tend to be the one bringing everyone together on the go outs.

Do you go or want to go to big gaming events like Blizzcon, MLG, Gamescon, E3, Dream Hack, etc?
YES! Money and time are my only limits. I finally made it to MLG last year even that my crappy car was broken. It was worth evey pennie.

Have you found starcraft or gaming in general to sometimes distract your from other commitments like work or school? If so how often does it happen?
I only play videogames because I don't want to do my work. If it wasn't gaming then I would find something else to proscastinate (like writing this paper)

Do you go to parties or social events outside of gaming like house parties, sports events, frat parties, clubs, etc.?
Yes. It mainly depends on if I enjoy the people going or not. If you know any house parties coming soon then let me know.

Information Questions
What is your current student status (freshmen, sophomore, etc.)?
Senior
Do you have a job?
Yes. I had 2 this semester but dropped one due to an awesome senior project that I want to put more time into it.
Are you a provider/supporter of a family (help pay bills, etc.)?
It is complicated. But yes I support myself and I didn't ask for money in over 5 years now. I am also expected to support my parents once I graduate school.
Thursday, November 3, 2011

An android App in one day (Under construction)

This blog will document my creation of an app in  2 days  one sitting. I will be using my notes on upcoming presentation that I will be giving in a UNCC student Club (C-Programming Union).

9:45 - I started a bit too late due to homework and lots of house shores. I need to work on my time planning better.

What you need:
Set up your enviorment:
I am cheating with this a bit since I have the IDE setup. But none of the steps are hard. Eclipse takes care of lots of the hard work. Use this link to have your up running and build on it.
http://developer.android.com/resources/tutorials/hello-world.html

I say follow the tutorial to get something running. My tutorial will follow the same steps but for another project as I think it would be a waste of time to write the tutorial again.

===
THE PROJECT!
For this project, I will do a greeting card app. The app will be the greeting card and I will just send it to my friend to tell him happy birthday. He is a nerd so he will understand.

Quick sketch of the final result!


LETS GET STARTED!
Create a new Android project following the steps on the hello world sitehttp://developer.android.com/resources/tutorials/hello-world.html#create
These are the Fields that I used ( withg comments //javastyle)

Project name: Greeting Cards //Name of the project
Target: Select a platform version that is equal to or lower than the target you chose for your AVD. (what version to work with. I just put 7 which is the same as 2.1. It doesn't matter much)
Application name:  Greeting Cards // The application name
name: com.cola.greetingcards // this is complicated. think of it as making your own package for use later. 
Create Activity: Main // your main class name


Once you fill up everything, Click on "Finished" and you will be directed to the main class.



package com.cola.GreetingCard;


import android.app.Activity;
import android.os.Bundle;


public class main extends Activity  //I made a mistake with the name of the class. Can you guess it?
{
    /** Called when the activity is first created. 
     * You will put in your code inside it. Think of it
     * As a constructor
     * */
@Override
    public void onCreate(Bundle savedInstanceState) 
    {
//inherit the onCreate from the on in the Activity class    
        super.onCreate(savedInstanceState);   
        //Show 
        setContentView(R.layout.main);
    }


}





Run your code and it would ask you where do you want to show the application. I use the emulator (if it is your first time, It would ask you to create an emulator environment. Just choose a 2.1 one or follow the instructions on the wiki website).

WORKING WITH XML!
Using XML can come pretty handy when creating the UI for android apps as it frees the programmer from the annoyance that comes with dealing with positioning. Separating the layout and display from the behind the scenes can save a lot of time. I hated having to deal with XML at the start but now I will never go back.


Jump to the main.xml file and change the values to make the app display what you want.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Happy Birthday Enrique"
    />
</LinearLayout>


Now run your application and it will display the new changes.
YES!

Now Lets try adding more resources( like text and images) on the canvas

Adding an image:
We need to put the image in the project somehow. We need to put the image in the drawable folder (there are 3 of them, for HD, small display and very small display. Just paste the image in all).

I added an image named android to the HD folder. Then I changed the main.XML by adding this after the text view


<ImageView android:layout_width="wrap_content"
android:layout_height="600px" 
android:src="@drawable/android" 
/>




Run your program and you will see this magic
THIS IS SOME CUTE STUFF

Adding interactivity
I tried adding sound but it doesn't seem as straight forward as I hoped to put in a tutorial. Instead, I will add a button and a toast.
And just to get it out of the way. Toast is a notification that appears to let the user know something. It looks like this:








So to add a button, We should go to the XML and add this part of the code under the ImageView block

<Button 
     android:id="@+id/button" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "Random Fact"
/>


You can compile your code. The button will be added. I will go and add the Toast before I compile.


So back to the main.java (man it have been a while. We didn't do anything in java for a while).

Insert this code in the toCreate block (under the part where you load the R.layout.main)


        //Code to handle what happens when we click on the buttons
        Button randomFact = (Button) findViewById(R.id.button);  //Create the button class
        
        //Create a listener that would detect any clicks and preform action
        randomFact.setOnClickListener(new View.OnClickListener() 
        {
        public void onClick(View v)
        {
        //This 1 line will display the toast
        Toast.makeText(getBaseContext(), "Enrique rocks!", Toast.LENGTH_LONG).show();
        }
        });


AND DONE!

It took 4 hours. I ran into a bug that I didn't understand with the button that just worked magically as well as a half hour or so trying to look into adding audio by using the XML but decided to not bother adding it here. Documenting also took some time with all the screen shots getting organized. I need to come back and clean up this blog a bit more.
Edit: I am aware that the "random fact" button is not really random. I think anyone who knows how to code in java would be able to do it. Let me know in the comments if you want me to add that code.


The app is located here:
http://dl.dropbox.com/u/6446381/android/GreetingCard.apk