是否可以将@Html.DropDownListFor
当前值传递给操作链接?我想使用 Create
操作将模板值传递给 Sample
Controller 。下面的代码不起作用,因为 @Model.SurveyTemplate
不返回任何值。是否需要JavaScript
来检索?请指导我。
下拉列表:
@Html.LabelFor(model => model.Survey_Template, "Survey Template")
@Html.DropDownListFor(model => model.Survey_Template, new SelectList(
new List<Object>{
new { value = "Style1" , text = "Style1" },
new { value = "Style2" , text = "Style2" },
new { value = "Style3" , text = "Style3"}
},
"value",
"text",
0))
操作链接:
@Html.ActionLink("_", "Create", "Sample",
new { template = @Model.Survey_Template },
new { @id = "popup-link", @class = "classname2" })
最佳答案
您必须使用 JavaScript
来执行此操作,razor 语法仅在服务器端被识别。这意味着@Model.Survey_Template
当用户请求页面时将呈现并具有默认值。
如果您选择使用 JavaScript
来执行此操作请记住,您需要保存基本 url 并根据所选内容向其附加参数。
此步骤将帮助您实现所需的结果:
- 附上
change
下拉菜单中的事件。 - 获取选定的
value
并构造新的 url - 替换操作 (
href
) 的网址 (<a .. />
)
注意:我写代码不是因为我想让你超越自己。
<小时/>我认为你试图实现的是 form
与 GET
.
@using(Html.BeginForm("Create", "Sample", FormMethod.GET)){
@Html.LabelFor(model => model.Survey_Template, "Survey Template")
@Html.DropDownListFor(model => model.Survey_Template, new SelectList(
new List<Object>{
new { value = "Style1" , text = "Style1" },
new { value = "Style2" , text = "Style2" },
new { value = "Style3" , text = "Style3"}
},
"value",
"text",
0))
<input type="submit" value="_" class="classname2" id="popup-link" />
}
关于javascript - 将当前值的 html.dropdownlist 传递给 Actionlink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30660367/