Passing Datas from cshtml to custom edit for not w

2019-07-13 12:16发布

问题:

This is my code in .cshtml. I want to pass value of css to my custom edit for but not working.

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

my code in custom edit for.

@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:

Well i reproduce your example on MVC 4 and i suppose you should just escape css property name in EditorFor hepler for then it works. Here you are:

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