2010年4月25日
- ○HTMLヘルパーを使ってフォームのコントロールを出力するときにIDの文字列を渡す必要がなくなった
- 今までは
- <%= Html.TextBox(“ProductName”, model.ProductName) %>
と書いて
<input id=”ProductName” name=”ProductName” type=”text” value=”xxxxxx” />
のように出力していたが、下記のようにIDの文字列を渡す必要がなくなった。
<%= Html.TextBoxFor(model => model.ProductName) %>
- ○モデルクラスのプロパティにAttributeをつけることによってバリデーションをサーバ、クライアントの双方でできるようになった
- System.ComponentModel.DataAnnotations名前空間下にある下記のAttributeが使用可能。
- [Required]、[StringLength]、[Range]、[RegularExpression]
- 以下使用例。
- [Required(ErrorMessage = "名前を入力してください。")]
[StringLength(50, ErrorMessage = "50文字以内です。")]
[Range(0, 120, ErrorMessage = "入力できる数字は0から120までです。")]
[ReqularExpression("^[0-9]{3}-?[0-9]{4}$”, ErrorMessage = “”]
- クライアントでもチェックできるようにするにはViewに
<script src=”Scripts/MicrosoftAjax.js” type=”text/javascript></script>
<script src=”Scripts/MicrosoftMvcValidation.js” type=”text/javascript></script>
<% Html.EnableClientValidation(); %>
- の3行を追加する。
- ADO.Net Entity Frameworkのようにツールで自動生成するモデルを使っている場合は、
自動生成時に属性がなくならないように下記のようにする。
- まずPartialクラスのファイルにMetadataType属性を追加する。
-
[[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}
- 次にメタデータクラスを追加して、プロパティに対してバリデーションを設定する。
-
[Bind(Exclude="ID")]
public class PersonMetadata
{
[Required(ErrorMessage = "名前を入力してください。")]
public string Name { get; set; }
}
-
ValidationAttributeクラスを継承したクラスを作ることによって、独自の検証属性を作ることも可能。
- ○Actionメソッドでデフォルト値を使えるようになった。
- {controller}/{action}/{id}
のようなUrlの時に、
public ActionResult View(int id, [DefaultValue(1)]int page) {
}
のような使い方が可能。
- ○非同期コントローラのサポート
- 非同期コントローラを使うことで負荷の高いサイトでサーバがスレッドプールを使い果たしてしまうのを防げる。
参考サイト
http://www.asp.net/learn/whitepapers/what-is-new-in-aspnet-mvc/
http://www.atmarkit.co.jp/fdotnet/scottgublog/20100113aspnetmvc2/aspnetmvc2.html
2010年4月16日
Windows上の仮想環境でLinuxを使用する場合、Linuxのハードウェアクロックの扱い方ををUTCからローカルタイムに変更しないと時刻がずれてしまう。
/etc/sysconfig/clockを編集し、「UTC=false」の行を「UTC=true」にして、リブートすれば変更完了。
2010年3月4日
WPF.Themesを使ってWPFでSilverlight ToolkitのようなThemeを使用することができる。現状21種類のテーマが提供されている。
- WPF.Themes.dllとWPFToolkit.dllを入手しそれぞれのDLLの参照を追加する。
- Windowタグに下記太字部分を追加する。(ThemeにShinyRedを使用する場合)
<Window x:Class=”WpfApplication1.Window1″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:themes=”clr-namespace:WPF.Themes;assembly=WPF.Themes”
themes:ThemeManager.Theme=”ShinyRed”
Title=”Window1″ Height=”300″ Width=”300″>
2010年2月23日
codecsモジュールを使用してファイルを読み込む。下記のコードで変数lineはUnicodeになる。
import codecs, sys
f = codecs.open(‘C:\\work\\utf8.txt’,mode=’r',encoding=’utf-8′)
for line in f:
sys.stdout.write(line)
f.close()
2010年2月23日
テンプレートファイルを作成する。
data1:{{ data1 }}
==============================
{% for val in list1 %} — {{ val }}
{% endfor %}
テンプレートファイルをおくパスをDjangoの設定に追加し、テンプレートをロードし、テンプレートに変数を渡す。
import django.conf
import django.template
import django.template.loader
# TEMPLATE_DIRSにテンプレートファイルをおくパスを指定してDjangoの手動設定を行う。
if django.conf.settings.configured == False:
django.conf.settings.configure(
DEBUG=True, TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=(‘D:/data/template1′, ‘C:/data/template2′))
#(なぜかパスは1つだけだとエラーになってしまう)
# テンプレートを取得する
tmpl = django.template.loader.get_template(‘test.txt’)
# テンプレートに渡す変数を設定して、レンダリングを行う。
print tmpl.render(django.template.Context({‘data1′ : ‘果物’,
‘list1′: (‘りんご’,'みかん’,'バナナ’,'レモン’) }))
2010年2月20日
IronPythonのインストールディレクトリにあるIronPython.dll, Microsoft.Scripting.dll, Microsoft.Scripting.Core.dll, Nicrosoft.Dynamic.dllに参照を通す。
下記のソースコードのような方法でC#のコードからPythonのスクリプトの実行と変数の値のやりとりが可能。
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
public class Class1
{
public IronPython.Runtime.List Val1;
public IronPython.Runtime.List Val2;
}
class Program
{
static void Main(string[] args)
{
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(
@”print obj1.Val1
result = obj1.Val2″);
ScriptScope scope = engine.CreateScope();
var obj1 = new Class1()
{
Val1 = new IronPython.Runtime.List { 1, “abc” },
Val2 = new IronPython.Runtime.List { 2, “def”}
};
scope.SetVariable(“obj1″, obj1);
source.Execute(scope);
object[] result = scope.GetVariable<IronPython.Runtime.List>(“result”).ToArray();
Console.WriteLine(“{0},{1}”, result[0], result[1]);
}
}
2010年2月20日
Django1.1.1をIronPython2.6にインストールし、importすると下記のようなエラーが発生する。
Cannot call lazy() with both str and unicode return types.
http://bitbucket.org/jdhardy/django-ironpython/changeset/b70eeacda60c/
を参考にして、django.utils.
assert not (cls._delegate_str and cls._delegate_unicode),
の行を
assert (str is unicode) or not (cls._delegate_str and cls._delegate_unicode),
に変更したらとりあえず回避できた。