【问题标题】:C# How to set the autopostback property when using asp.net mvc?C#使用asp.net mvc时如何设置autopostback属性?
【发布时间】:2010-10-18 08:44:48
【问题描述】:

我正在使用 asp.net MVC 框架。在我的页面上,我有一个 dropdwonbox,当单击一个选项时,我想转到另一个页面。但我找不到如何/在哪里将 autopostback 属性设置为 true。这是我正在使用的代码:

aspx:

<%= Html.DropDownList("qchap", new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" )) %>

控制器:

public ActionResult Index(int id)
{
    Chapter c =  new Chapter();
    ViewData["qchap"] = c.GetAllChaptersByManual(id);

    return View();
}

我必须做什么才能使用自动回发功能?

【问题讨论】:

    标签: c# asp.net-mvc combobox autopostback


    【解决方案1】:

    我用这段代码解决了。

    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
    

    这就是 ActionResult 函数

    然后是视图

     <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>
    

    我希望它有所帮助。如果您想要更完整的代码,请发邮件给我boylevantz@gmail.com

    【讨论】:

      【解决方案2】:

      我也相信您可能希望将回发调整为 formsCollection

      postback public ActionResult Index(FormsCollection myform)

      (我不在安装MVC的家用电脑上,所以这里无法验证语法)

      【讨论】:

        【解决方案3】:

        似乎 DropDownList 辅助方法不支持这一点。 也许在表单中使用它并使用自定义的自定义 html 属性来提交表单。

        【讨论】:

          【解决方案4】:

          您可以使用 onchange 客户端事件:

          <%= Html.DropDownList("qchap", 
                 new SelectList( (IEnumerable)ViewData["qchap"], "Id", "Title" ),
                 new { onchange = "this.form.submit();" }) %>
          

          【讨论】:

          • 谢谢。如果我想添加类属性,我是否必须使用相同的方式?
          • 是的,尽管使用 c# 你需要在前缀下划线.. 即 new { _class= "something" }
          • 控制器如何知道需要执行哪个动作?
          猜你喜欢
          • 2011-11-01
          • 2016-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多