C# 如何在使用 asp.net mvc 时设置 autopostback 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/730800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How to set the autopostback property when using asp.net mvc?
提问by Martijn
I am using asp.net MVC framework. On my page i have a dropdwonbox and when an option is clicked i want to go to another page. But i can't find how/where to set the autopostback property to true. This is the code i'm using:
我正在使用 asp.net MVC 框架。在我的页面上,我有一个 dropdwonbox,当单击一个选项时,我想转到另一个页面。但我找不到如何/在哪里将 autopostback 属性设置为 true。这是我正在使用的代码:
Aspx:
ASP:
<%= Html.DropDownList("qchap", new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" )) %>
Controller:
控制器:
public ActionResult Index(int id)
{
Chapter c = new Chapter();
ViewData["qchap"] = c.GetAllChaptersByManual(id);
return View();
}
What do i have to do to use the autopostback functionality?
我需要做什么才能使用自动回发功能?
采纳答案by CMS
You can use the onchange client event:
您可以使用 onchange 客户端事件:
<%= Html.DropDownList("qchap",
new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" ),
new { onchange = "this.form.submit();" }) %>
回答by Sebastian
It seems the DropDownList helper method doesn't support this. Maybe using it within a form and a custom custom html attribute to submit the form do it.
DropDownList 辅助方法似乎不支持此功能。也许在表单中使用它和自定义自定义 html 属性来提交表单。
回答by James Fleming
I believe too that you may want to adjust your postback to the formsCollection
我也相信您可能希望将回发调整为 formsCollection
postback public ActionResult Index(FormsCollection myform)
回发公共 ActionResult 索引(FormsCollection myform)
(I'm not on my home pc where MVC is installed, so I can't verify the syntax here)
(我不在安装了 MVC 的家用电脑上,所以我无法在这里验证语法)
回答by Ali Irawan
I solve using this code.
我使用此代码解决。
Function Index(ByVal collectionField As FormCollection) As ActionResult
Dim industryCategoryID As Long = collectionField.Item("ddlIndustry")
If industryCategoryID = 0 Then
Me.ViewData("IndustryList") = GlobalController.GetIndustryList
Return View(_service.ListCompanies())
Else
Me.ViewData("IndustryList") = GlobalController.GetIndustryList
Return View(_service.ListCompanies(industryCategoryID))
End If
End Function
That's for the ActionResult function
那是 ActionResult 函数
And Then for the View
然后为视图
<p>
<% Using Html.BeginForm()%>
<%=Html.DropDownList("ddlIndustry", New SelectList(CType(ViewData("IndustryList"), IEnumerable), "ID", "Name"), "--Choose industry--", New With {.onchange = "this.form.submit()"})%>
<% End Using %>
</p>
I hope it helps. I f you would like more complete codes please feel good to email me at [email protected]
我希望它有帮助。如果您想要更完整的代码,请发送电子邮件至[email protected]