当我的模型是在我的ASP.NET MVC应用程序空EditorFor(..)抛出一个异常 [英] EditorFor(..) throws an exception when my model is null in my ASP.NET MVC application

查看:162
本文介绍了当我的模型是在我的ASP.NET MVC应用程序空EditorFor(..)抛出一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的ASP.NET MVC查看里面有一个简单的字符串作为模型。

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

如:

@model字符串

当我这样做时,该视图抛出一个异常,如果模型值为(即当这一观点/页/资源的用户第一次土地)

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>

我怎样才能创建一个输入框使用 EditorFor(..)当字符串的模式值为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.

这篇关于当我的模型是在我的ASP.NET MVC应用程序空EditorFor(..)抛出一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆