将数据从cshtml传递到自定义编辑,无法正常工作

时间:2015-10-29 08:17:55

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

这是.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})

1 个答案:

答案 0 :(得分:1)

我在MVC 4上重现您的示例,我想您应该在css hepler中转义EditorFor属性名称,然后才能正常工作。你在这里:

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