当我的模型在我的ASP.NET MVC应用程序中为null时,EditorFor(..)抛出异常

Pur*_*ome 5 asp.net asp.net-mvc razor

我有一个非常简单的ASP.NET MVC View,它有一个简单string的模型.

例如.

@model string

当我执行以下操作时,如果模型值为null(当用户首次登陆此视图/页面/资源时发生),视图将引发异常.

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>
Run Code Online (Sandbox Code Playgroud)

当字符串模型值为null时,如何创建input box使用EditorFor(..)

Tim*_*ple 5

问题似乎是它无法确定它应该用作您正在创建的字段的名称.我建议传入一个带有单个字符串属性的ViewModel,而不是直接传递一个字符串.如果有必要,这将使您有机会使用数据注释来提供有关该字段的其他数据.