【问题标题】:Asp.net 4.5 WebForms ModelBinding not allowing setting selectedvalue on dropdownlistAsp.net 4.5 Web Forms 模型绑定不允许在下拉列表中设置 selectedvalue
【发布时间】:2013-04-24 14:58:49
【问题描述】:

我正在使用 Asp.net 4.5 模型绑定。我有一个下拉列表,它有一个返回 IEnumerable 产品列表的选择方法。一切都按预期工作。但是,当我登陆页面时,我需要检查查询字符串并更改下拉列表的选定值(使用 SelectMethod 绑定。

通常我会简单地使用...

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlProducts.SelectedValue = Request.QueryString["productid"];
        }
    }

但是,当下拉列表绑定到 SelectMethod(新的 asp.net 4.5 模型绑定)时,这不起作用。那么,当我登陆页面时,如何通过查询字符串设置下拉列表的选定值?

ASPX(使用普通下拉列表,但带有 SelectMethod):

<asp:DropDownList ID="ddlProducts" runat="server" DataTextField="Description" DataValueField="ProductId" AppendDataBoundItems="True" SelectMethod="GetProducts"></asp:DropDownList>

CS

public static IEnumerable<Product> GetProducts()
{
    using (var db = new AppDb())
    {
        return db.Products.OrderBy(x => x.Description).ToList().AsEnumerable();
    }
}

【问题讨论】:

    标签: asp.net binding model selectedvalue selectmethod


    【解决方案1】:

    我不是专家,所以您的里程可能会有所不同。 但我认为你所要做的就是改变你对 getProducts 的定义: 来自

    public static IEnumerable<Product> GetProducts()
    

    public static IEnumerable<Product> GetProducts([QueryString("somequerystringvariablename")] string param)
    

    然后使用“参数”过滤您的查询

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-07
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多