简体   繁体   English

值不能为null或为空。\ r \ nParameter name:name

[英]Value cannot be null or empty.\r\nParameter name: name

when i go to cshtml page this error occur: 当我去cshtml页面时出现这个错误:

An exception of type 'System.ArgumentException' occurred in System.Web.Mvc.dll but was not handled in user code System.Web.Mvc.dll中出现“System.ArgumentException”类型的异常,但未在用户代码中处理

in my cshtml code : 在我的cshtml代码中:

<div class="form-group">
     @Html.Label("Model", new { @class = "control-label col-md-4" })
  <div class="col-md-8">
     @Html.Editor("Model")
  </div>
</div>

在此输入图像描述

but on the other hand if i change my code in @Html.Editor("PropertyModel") 但另一方面,如果我在@Html.Editor("PropertyModel")更改我的代码

it is working perfectly no error occur . 它完美地工作没有错误发生。

UPDATE: 更新:

in my model: 在我的模型中:

public class PropertyAssign {
        public int PropertyAssignId { get; set; }

        public string PropertyName { get; set; }

        public string Model { get; set; }


        public DateTime AssignDate { get; set; }
    }

In My Controller: 在我的控制器中:

 public ActionResult Create()
    {
      return View();
    }

I've tryed this case and it's very strange, but there is some problem with the identifier of your Model property with usage of @Html.Editor() helper. 我尝试过这种情况,这很奇怪,但是使用@ Html.Editor()帮助器时,Model属性的标识符存在一些问题。

@Html.Editor("Model") // throws an exception
@Html.EditorFor(m => m.Model) // works fine

Maybe in this context the meaning of Model is the whole model, which is passed to the view, not the single property. 也许在这种情况下,Model的含义是整个模型,它被传递给视图,而不是单个属性。 But when I've changed the identifier of the Model property to some other, for example SomeModel, everything is fine. 但是当我将Model属性的标识符更改为其他属性时,例如SomeModel,一切都很好。

@Html.Editor("SomeModel") // works fine
@Html.EditorFor(m => m.SomeModel) // works fine

So, the possible solutions are: 因此,可能的解决方案是:

  • to change the Editor helper you use or 更改您使用的编辑器助手或
  • to change your property identifier 更改您的属性标识符

The reason the exception is thrown is that the HtmlHelpers inspect ViewData to get the values to bind to. 抛出异常的原因是HtmlHelpers检查ViewData以获取要绑定的值。 ViewData is typeof ViewDataDictionary which contains a property Model (which is the model you pass to the view). ViewDataViewDataDictionary的类型 ,它包含一个属性Model (它是传递给视图的模型)。

In your case, you do not return a model to the view so ViewData.Model is null and the exception is thrown. 在您的情况下,您不会将模型返回到视图,因此ViewData.Modelnull并抛出异常。

To solve this, use the strongly typed EditorFor(m => m.Model) (or TextBoxFor(m => m.Model) which you should be doing in any case. Even if you do not pass a model to the view, this will still generate the correct html without throwing an exception. 要解决这个问题,请使用强类型的EditorFor(m => m.Model) (或TextBoxFor(m => m.Model)TextBoxFor(m => m.Model)你都应该这样做。即使你没有将模型传递给视图,这个仍然会生成正确的HTML而不会抛出异常。

<input type="text" name="Model" id="Model" value="" />

but its best practice to pass a model anyway, so your controller method should be 但无论如何都是传递模型的最佳实践,所以你的控制器方法应该是

public ActionResult Create()
{
  return View(new PropertyAssign());
}

write using your model name at the top like 使用您的模型名称在顶部写

@using projectNamespace.PropertyAssign 

when using editor for your some property you can write like 当您使用编辑器为您的某些属性时,您可以像写一样

@Html.EditorFor(m => m.PropertyName) 

for text box its 对于文本框它

@Html.TextBoxFor(model => Model.PropertyAssign , new { @class = "release-prefix" @id = "prefix" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM