4

ASP.NET MVC でデータを送信するためのフォームを作成する最良の方法は何ですか? Scott Guthrieがここで示しているとおりですか? より良いアプローチはありますか?おそらく文字列の使用が少ないのでしょうか?

代替テキスト

4

1 に答える 1

2

リファクタリングできないため、コード内の文字列はあまり好きではありません。良い方法は、Linq 式を使用することです。モデルを ViewData として渡された場合は、次のステートメントを使用できます。

<%= ShowDropDownBox(viewData => viewData.Name); %>
...

public static string ShowDropDownList<T>(this HtmlHelper html, Expression<Action<T>> property)
{
    var body = action.Body as MethodCallExpression;
    if (body == null)
        throw new InvalidOperationException("Expression must be a method call.");
    if (body.Object != action.Parameters[0])
        throw new InvalidOperationException("Method call must target lambda argument.");
    string propertyName = body.Method.Name;
    string typeName = typeof(T).Name;

    // now you can call the original method
    html.Select(propertyName, ... );
}

元のソリューションの方がパフォーマンスが速いことは知っていますが、これははるかにクリーンだと思います。

お役に立てれば!

于 2008-08-20T19:47:21.250 に答える