【问题标题】:Passing Datas from cshtml to custom edit for not working将数据从 cshtml 传递到自定义编辑表单不起作用
【发布时间】:2015-10-29 08:17:54
【问题描述】:

这是我在.cshtml 中的代码。我想将 css 的值传递给我的自定义编辑但不工作。

 @Html.EditorFor(model => model.xxx, new { size = 20, maxLength = 10 , css = "test"  })

我的代码在自定义编辑中。

@model string

@{
    int size = 50;
    int maxLength = 50;
    string type = "text";
    string css = "";

    if (ViewData["size"] != null)
    {
        size = (int)ViewData["size"];
    }
    if (ViewData["maxLength"] != null)
    {
        maxLength = (int)ViewData["maxLength"];
    }
    if (ViewData["type"] != null)
    {
        type = (string)ViewData["type"];
    }
    if (ViewData["css"] != null)
    {
        css = (string)ViewData["css"];
    }
}
@Html.TextBox("", Model, new { Size = size, MaxLength = maxLength, Type = type, @class = css})

【问题讨论】:

  • 你使用什么版本的 MVC?而你的model.xxx 是string 类型?
  • 是的,它是一个字符串类型。换个标签

标签: asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

好吧,我在 MVC 4 上重现了您的示例,我想您应该在 EditorFor hepler 中转义 css 属性名称,这样它就可以工作了。你在这里:

@Html.EditorFor(model => model.xxx, new { size = 20, maxLength = 10 , @css = "test"  })

【讨论】:

  • 是保留字吗?
  • @DerpukuDer 我认为 - 是的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多