开发者

EditorFor(..) throws an exception when my model is null in my ASP.NET MVC application

I've got a really simple ASP.NET MVC View which has a simple string as the model.

eg.

@model string

When I do the following, the view throws an exception, if the model value is null (which occurs when the user first lands on this view/page/resource).

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>

How 开发者_JS百科can I create an input box using EditorFor(..) when the string-model value is null?


It appears the problem is that it cannot determine what it should use as the name of the field that you are creating. I would recommend passing in a ViewModel with a single string property in it instead of just passing a string directly. This will give you an opportunity to use data annotations to provide additional data about the field if necessary as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜