2011-09-02 23 views
5

シンプルなASP.NET MVCビューを持っていますが、モデルとしてはstringがシンプルです。ASP.NET MVCアプリケーションでモデルがnullのときにEditorFor(..)が例外をスローする

例えば、私は次のことを行うとモデル値がnullある場合

@model string

は、ビューは、(発生したときに、このビュー/ページ/リソース上のユーザー第1ランド)、例外がスローされます。

Value cannot be null or empty. 
Parameter name: name 

Description: An unhandled exception occurred during the execution of the 
      current web request. Please review the stack trace for more 
      information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Value cannot be null or empty. 
Parameter name: name 

<td>@Html.EditorFor(model => model)</td> 

どのように私は、文字列モデルの値がnullのときEditorFor(..)を使用してinput boxを作成することができますか?

+1

できません。フィールド名はどのような名前になりますか? – bzlm

答えて

4

問題は、作成するフィールドの名前として使用する必要があるかどうかを判断できないようです。文字列を直接渡す代わりに、単一の文字列プロパティを持つViewModelを渡すことをお勧めします。これにより、必要に応じてデータ注釈を使用してフィールドに関する追加データを提供する機会が与えられます。

関連する問題