1

我有一个场景,我将添加两个属性。

一个是动态的,即基于用户角色的只读,另一个是静态的,即id

我在下面尝试了这个,但它对我不起作用。

下面在 HTML 中呈现的属性是

readonlystr = , id = 'emp-name'

var readonlystr = (Model.Role.EmpEdit) ? "" : "readonly=readonly";

@Html.TextBoxFor(m => m.Name, new { readonlystr, @id = "emp-name" })
@Html.TextBoxFor(m => m.Age, new { readonlystr, @id = "emp-age" })
@Html.TextBoxFor(m => m.City, new { readonlystr, @id = "emp-city" })

有什么建议可以通过组合两个属性并通过它来实现这一点吗?

4

2 回答 2

1
var readonlystr = Model.Role.EmpEdit ? new Dictionary<string, object>() : new Dictionary<string, object> { { "readonly", "readonly" } };

@Html.TextBoxFor(m => m.Name, readonlystr.Union(new Dictionary<string, object>{{"id", "emp-name"}}).ToDictionary(x=>x.Key, x=>x.Value))
于 2013-01-18T12:46:37.153 回答
0
    @{
        Dictionary<string, object> attribs = new Dictionary<string, object>();
        if (!Model.Role.EmpEdit) {
            attribs.Add("readonly", "readonly");
        }
        attribs.Add("id", "");
    }

    @{attribs["id"] = "emp-name";}
    @Html.TextBoxFor(m => m.Name, attribs)
    @{attribs["id"] = "emp-age";}
    @Html.TextBoxFor(m => m.Age, attribs)
于 2013-01-18T12:46:53.467 回答