我有一系列DropDowns,我希望用户添加和编辑。我从StackOverflow找到了一个帮助扩展程序来构建一个动作图像链接。
@Html.DropDownListFor(model => model.Entry.ParadigmId, ((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Name),
Value = option.ParadigmId.ToString(),
Selected = (Model != null) && (option.ParadigmId == Model.Entry.ParadigmId)
}), "Select")
@Html.ActionImage("ParadigmEdit", new { id = ? }, "~/Content/Images/Edit_Icon.gif", "ParadigmEdit")
我不知道如何在DropDownList中引用选定的id值,问号在上面的代码中。
答案 0 :(得分:1)
您无法使用服务器端代码(HTML帮助程序代表哪些代码)从下拉列表中引用所选值,因为选择是由用户在客户端完成的。您的问题源于这样一个事实:您正在尝试生成一个锚点,该锚点应该发送仅由客户端知道的值。你只能使用javascript来做到这一点。或者另一种可能性是简单地使用带有图像提交按钮的表单:
@using (Html.BeginForm("ParadigmEdit", "ControllerName"))
{
@Html.DropDownListFor(
model => model.Entry.ParadigmId,
// WARNING: this code definetely does not belong to a view
((IEnumerable<Pylon.Models.Paradigm>)ViewBag.PossibleParadigms).Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Name),
Value = option.ParadigmId.ToString(),
Selected = (Model != null) && (option.ParadigmId == Model.Entry.ParadigmId)
}),
"Select"
)
<input type="image" alt="ParadigmEdit" src="@Url.Content("~/Content/Images/Edit_Icon.gif")" />
}
当然,在您移动它所属的丑陋代码(映射层或视图模型)后,您的代码将变为:
@using (Html.BeginForm("ParadigmEdit", "ControllerName"))
{
@Html.DropDownListFor(
model => model.Entry.ParadigmId,
Model.ParadigmValues,
"Select"
)
<input type="image" alt="ParadigmEdit" src="@Url.Content("~/Content/Images/Edit_Icon.gif")" />
}