繁体   English   中英

MVC5 Razor 值不能为 null 或为空。 参数名称:名称

[英]MVC5 Razor Value cannot be null or empty. Parameter name: name

源错误:第 22 行:@Html.TextBoxFor(model => model.DailyCurrencyRates___BuyingCurrency, new { @id = "test", @class = "form-control" })

我被这个问题困了好几天,无法解决这个错误。 我只想用父类的项目呈现部分视图。 以下是我的代码:如果需要,请询问更多信息。

控制器

public ActionResult AddRecord(int index)
{
  return PartialView("_AddItem", new ForexItem { Index = index});
}

父视图

@foreach (ForexItem item in Model.ForexItems)
{
    Html.RenderPartial("_AddItem", item);
}

局部视图

@model F.Models.Entities.ForexItem

@Html.TextBoxFor(model => model.DailyCurrencyRates___BuyingCurrency, new { @id = "test", @class = "form-control" })

模型

public partial class ForexItem
 {
        public int ID { get; set; }

        [NotMapped]
        [StringLength(1000)]
        [Display(Name = "Buying")]
        public string DailyCurrencyRates___BuyingCurrency { get; set; }

        [NotMapped]
        public int Index { get; set; }
}

当我将模型属性名称中的下划线数量从 1 增加到 2 时,我得到了同样模糊、模糊的错误。请注意,在 TextBoxFor 助手之前的 LabelFor Helper 没有发生该错误,而只是在 TextBoxFor 助手中,相同如你所见。 (我注意到您的属性名称(“DailyCurrencyRates___BuyingCurrency”)有 3 个下划线。)然后当我从属性名称中删除额外的下划线时它就消失了。 好像有了额外的下划线,Razor 找不到属性名称,所以它只是搜索(通用)“名称”,它也找不到。

这可能是一个错字,但在我看来,您应该将Model.ForexItems更改为Model.ForexItem - 没有。 根据您提交的代码。 ……或者也许只是使用Model因为您已经将 ForexItem 放入其中?

父视图

@foreach (ForexItem item in Model)
{
    Html.RenderPartial("_AddItem", item);
}

暂无
暂无

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

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