我正在尝试在HTML select选项中嵌入Ajax.ActionLink
,如下所示
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"</script>
<select>
<option>@Ajax.ActionLink("Opiton 1", "MyAction", new { parameter = "parm1" },
new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace,
LoadingElementId = "myDiv" })</option>
<option>@Ajax.ActionLink("Opiton 2", "MyAction", new { parameter = "parm1" },
new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace,
LoadingElementId = "myDiv" })</option>
</select>
<div id="myDiv"></div>
行动方法
public ActionResult MyAction(string parameter)
{
ViewModel myViewModel = new ViewModel();
myViewModel.Parm = parameter;
if (Request.IsAjaxRequest())
{
return PartialView("_MyPartial", myViewModel);
}
else
{
return View(myViewModel);
}
}
当我将Ajax.ActionLink
放在选择列表之外时,它可以正常工作并直接进入MyAction。当我在选择列表中的选项中嵌入链接时,onlclick事件无法触发。
如何在<option></option>
内部触发onclick事件?
答案 0 :(得分:3)
为什么select元素中有2个Action链接。我想你想调用一个action方法并将返回值设置为div。我不会使用Microsoft ajax库。我会用这样的干净的代码手工完成。(我只需要在页面中引用jQuery库。没有别的)
//include only jQuery library. Nothing else !
<select id="dropDown1">
<option value="0"> Select</option>
<option value="1"> Detroit</option>
<option value="2"> Florida</option>
</select>
<script type="text/javascript">
$(function(){
$("#dropDown1").change(function(){
$.get("@Url.Action("MyAction", "MyController")",
{ "parameter" : $(this).val()},function(data){
$("#myDiv").html(data);
});
});
});
</script>