代码之家  ›  专栏  ›  技术社区  ›  user2906420

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

  •  -2
  • user2906420  · 技术社区  · 7 年前

    源错误:第22行:@Html.TextBoxFor(型号=> model.DailyCurrencyRates\uuu Buying货币,新建{@id=“test”,@class

    我在这个问题上耽搁了好几天,无法绕过这个错误。我只想呈现一个包含父类项的部分视图。以下是我的代码:如果需要,请询问更多信息。

    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 回复  |  直到 7 年前
        1
  •  3
  •   apswrk    7 年前

    当我将模型属性名中的下划线数量从1增加到2时,我得到了同样模糊、模糊的错误。请注意,labelforhelper并没有直接在TextBoxFor Helper之前出现,而是在TextBoxFor Helper中发生,与您得到的相同。(我注意到您的物业名称(“DailyCurrencyRates\uuuBuyingCurrency”)有3个下划线。)然后当我从属性名称中删除多余的下划线时,它就消失了。好像有了额外的下划线,Razor找不到属性名,所以它只搜索(通用的)“name”,但也找不到。

        2
  •  1
  •   Lasse Holm    7 年前

    这可能是个打字错误,但在我看来你应该改变一下 Model.ForexItems 到 Model.ForexItem 或者只是使用 Model 既然你已经把它放进去了?

    父视图

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