我们一般把通用的组件放在Shared文件夹下
@{
var model = ViewData["dataSource"] as IDictionary<string, string>;
var selectedItem = ViewData["selected"] == null ? string.Empty :ViewData["selected"].ToString();
}
<select class="@ViewData["className"] SelectWrapper" id="@ViewData["id"]" name="@ViewData["name"]" >
<option value="">--请选择--</option>
@foreach (var item in model) {
string selected = selectedItem == item.Key ? "selected =\"selected\"" : string.Empty;
<option value="@item.Key" @selected>@item.Value</option>
}
</select>
页面应用: @Html.Partial(Config.SelectControlView, new ViewDataDictionary() {
{"dataSource", inputModel.ALLSaveModelSelectDataSource},
{"selected", -1},
{"for", "SaveModel"},
{"id","savemode"},
{"name","savemode"}
})
Config.SelectControlView的对应值为:
public const string SharedFolder = "~/Areas/Sample/Views/Shared/";
public static string SelectControlView = Path.Combine(SharedFolder, "Controls/_Select.cshtml");