2016年9月21日 星期三

[ASP.NET MVC] @Html.Action In Layout Page

遇到就筆記一下…


In the _ViewStart
Set Layout = null or mark it.
@{
    Layout = null;  //Layout = "~/Views/Shared/_Layout.cshtml";
 }



In the _Layout.cshtml 

Use @Html.Action(actionName, controllerName)
 <div id="header">
        @Html.Action("HeadMenu", "Home")
 </div>


In the Controller 
  
  public class HomeController : Controller
    {
        public ActionResult HeadMen()
        {
            //TODO something...
            return View();
        }
   }




http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts

2016年9月20日 星期二

[C#語言入門] explicit (明確轉型) vs. implicit (隱含轉型)

Just a note to myself...

public static explicit operator Monkey(Stone stone) {  }
public static implicit operator Monkey(Stone stone) {  }


explicit MSDN


       static void Main(string[] args)
        { 
            Stone stone = new Stone();
            Monkey m = (Monkey)stone;
            m.Age = 5000;
            Monkey wukongSun = (Monkey)stone; // explicit 明確轉型 (Monkey)
            //Monkey wukongSun = stone;              // implicit 隱含轉型
            Console.WriteLine(wukongSun.Age);
        }




  class Stone
    {
        public int Age;
        
        // Monkey wukongSun = (Monkey)stone;
        public static explicit operator Monkey(Stone stone)
        {
            Monkey m = new Monkey();
            m.Age = stone.Age / 500;
            return m;
        }
    }

    class Monkey
    {
        public int Age;
    }





implicit  MSDN


        // Monkey wukongSun = stone;
        public static implicit operator Monkey(Stone stone)
        {
            Monkey m = new Monkey();
            m.Age = stone.Age / 500;
            return m;
        }





https://www.youtube.com/watch?v=q6yAgdzGAJo

2016年9月19日 星期一

[C#語言入門] 前置++/-- vs. 後置++/--


Just a note to myself... (觀念很重要!!!)


1. 後置++ (x++):

後置++,當 x 發現左邊是賦值符號時,把自己當成了值,先交給了賦值變數 y (y = x),再進行自加(x = x + 1)。

            int x = 100;
            int y = x++;
            Console.WriteLine(x); //x = 101
            Console.WriteLine(y); //y = 100


int y = x++;
相當於…
y = x;
x = x + 1;


2. 前置++ (++x):

前置++,當 x 發現左邊是賦值操作時,先++( x = x+1), 再交給y。

            int x = 100;
            int y = ++x;
            Console.WriteLine(x); //x = 101
            Console.WriteLine(y); //y = 101


int y = ++x;
相當於…
x = x + 1;
y = x;



https://www.youtube.com/watch?v=LA8V9DlLJ10

[C#語言入門] 將匿名方法(delegate) 改寫為 Lambda 表達式


Just a note to myself...



Lambda 表達式 

            //Lambda表達式
            this.myButton.Click += ( sender, e) =>
            {
                this.myTextBox.Text = "Hello, World!";
            };
       



Step 1: 匿名方法(使用delegate)的用法 => 該方法己過時


Modify from 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.myButton.Click += myButton_Click;  
        }

        void myButton_Click(object sender, RoutedEventArgs e)
        {
            this.myTextBox.Text = "Hello, World!";
        }
    }

To
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //使用"匿名方法" => 該方法己過時
            this.myButton.Click += delegate(object sender, RoutedEventArgs e)
            {
                this.myTextBox.Text = "Hello, World!";
            };
        }
    }



Step 2: 改寫為 Lambda 表達式 

1. 將 "delegate" 拿掉。
2. 將 (object sender, RoutedEventArgs e) 參數類型拿掉。
3. 承上,在後面加上Lambda表示式 "=>"。


  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //Lambda表達式
            this.myButton.Click += ( sender, e) =>
            {
                this.myTextBox.Text = "Hello, World!";
            };
        }
    }




For test UI (WPF)

<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="AliceBlue">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="2"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="myTextBox" Grid.Row="0"/>
        <Button x:Name="myButton" Content="Click Me!" Grid.Row="2"/>
    </Grid>
</Window>



https://www.youtube.com/watch?v=LA8V9DlLJ10


[C#語言入門] 使用 checked/ unchecked 檢查是否溢出(OverflowException)

Just a note to myself...MSDN


1. 用 try{ } catch(OverflowException){ } 包起來, 運算式用 check/ uncheck包起來

 checked ( 運算式 )/ unchecked ( 運算式 )

           try
            {
                uint y = unchecked(x + 1); //unchecked: 不檢查
                //uint y = checked(x + 1);   //checked: 檢查 -> Overflow -> OverflowException
                Console.WriteLine(y);

            }
            catch (OverflowException ex)
            {
                Console.WriteLine("There's overflow!");
            }
            



2. 使用 checked { } / uncheck{ } 檢查是否異常

checked{ } (拋出溢出) /  unchecked { } (不拋出溢出)

           checked
            {
                try
                {
                    uint y = x + 1;
                    Console.WriteLine(y);
                }
                catch (OverflowException ex)
                {
                    Console.WriteLine("There's overflow!");
                } 
            }





For test:


           uint x = uint.MaxValue;
            Console.WriteLine(x);
            string binStr = Convert.ToString(x, 2); //result: 32位個1
            Console.WriteLine(binStr);

            //uint y = x + 1; //會溢出
            //Console.WriteLine(y); //result = 0


[C#語言入門] 匿名類型(Anonymous Type)


Just a note to myself… MSDN


匿名類型(Anonymous Type)

            //匿名類型(Anonymous Type) : new 後面,不寫任何數據類型
            var person = new { Name = "Mr.Okay", Age = 34 }; / /使用var讓編譯器自己去推斷是什麼類型
            Console.WriteLine(person.Name);
            Console.WriteLine(person.Age);
            Console.WriteLine(person.GetType().Name);


非匿名類型(non-Anonymous Type)


            //非匿名類型 :new 後面一定要跟著數據類型 
            Form myForm = new Form() { Text = "Hello"};



※ 用 new 功能強大,但要非常小心,用不好會過於依賴注入。
※ 使用var,可以讓編譯器自己去推斷是什麼類型。

[ASP.NET MVC] Html Helper Extensions


Just a note to myself...

Step 1: Custom Html Helper Extensions


namespace MvcExample.Extensions
{
    public static class WaspecExtentions
    {
        public static MvcHtmlString H2(this HtmlHelper htmlHelper, string content)
        {
            TagBuilder tagBuilder = new TagBuilder("h2");
            tagBuilder.InnerHtml = content;
            MvcHtmlString result = MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));  //<h2></h2>
            return result;
        }

        public static MvcHtmlString Hehehe(this HtmlHelper htmlHelper, string name, object value)
        {
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttribute("id", name);
            tagBuilder.MergeAttribute("name", name);
            tagBuilder.MergeAttribute("type", "text");
            tagBuilder.MergeAttribute("value", value.ToString());
            tagBuilder.MergeAttribute("class", "blueTextBox");
            MvcHtmlString result = MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));  //<input .../>
            return result;
        }
    }
}



Step 2: Use them


    @Html.H2("How are you!")
    @Html.Hehehe("textBox2", "Hello!")




※ Other note for me:

    @Html.TextBox("textBox1", DateTime.Now, new { Blahblah="bbb", @class="blueTextBox"})<br/>
    <input type="text" id="textBox3" name="textBox3" value="@DateTime.Now" class="blueTextBox" />
    @Html.CheckBox("checkBox1",true) Are you ASP.NET MVC student?