ASP.NET MVC DropDownList的选定值问题 [英] ASP.NET MVC DropDownList Selected Value Problem

查看:179
本文介绍了ASP.NET MVC DropDownList的选定值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一吨这个问题,但我不能弄明白。

I have read a ton about this problem, but I cannot figure it out.

我有我的观点,即用于寻呼一个DropDownList。你选择你想要去的网页和表单被提交。当返回视图,示出了所请求的页面。 DropDownList的变化,以反映新显示的页面。这是工作的罚款。

I have a dropdownlist on my view that is used for paging. You choose the page you want to go to and the form gets submitted. When the view is returned, the requested page is shown. The dropdownlist changes to reflect the newly shown page. This is working fine.

我的问题是这样的: 我有2个提交按钮,一个是下一个页面,一个是$ P $光伏页。 在控制器中,如果的提交之一已经pssed $ P $,页#应增加或减少1。这是发生,并且数据的正确的页面被显示。但是,在下拉列表不会,不管我做什么,反映新的页码的视​​图显示时。

My problem is this: I have 2 submit buttons, one for next page and one for prev page. In the controller, if one of the submits have been pressed, the page# should be incremented or decremented by 1. This is happening, and the correct page of data is being shown. But, the dropdown list will not, NO MATTER WHAT I DO, reflect the new page number when the view is shown.

基本上,如果下拉被用户改变,这将反映这些变化时,它是一种形式提交后发回。如果我更改网页#编程,下拉列表将不会改变,即使数据的正确的页面被显示。

Basically, if the dropdown is changed by the user, it will reflect those changes when it is sent back after a form submission. If I change the page # programmatically, the dropdown will not change, even though the correct page of data gets shown.

下面是操作方法:

public ActionResult Results(TechSearch search, string NextPage, string PrevPage)
{
    if (NextPage != null) search.Page++;
    if (PrevPage != null) search.Page--;

    int resultCount = search.GetResultCount(Desk);

    List<int> pages = new List<int>();
    int pageCount = (int)Math.Ceiling((decimal)resultCount / search.PageSize);
    for (int i = 1; i <= pageCount; i++) pages.Add(i);        

    ViewData["pages"] = new SelectList(pages, search.Page);            
    ViewData["pageCount"] = pageCount;

    return View(search);
}

这里是视图的相关部分:

And here is the relevant part of the view:

<input type="submit" value="<" name="PrevPage" />
Page
<%=Html.DropDownList("Page",(SelectList)ViewData["pages"]) %>
of
<%=ViewData["pageCount"]%>
<input type="submit" value=">" name="NextPage"  />

请帮忙。

临ASP.NET MVC框架333由史蒂芬·桑德森

如何输入控件获取值

每个的[的HTML辅助的渲染输入控件]尝试   通过查找值填充本身   在以下位置,按此顺序   优先级:

Each of [the HTML Helpers for Rendering Input Controls] tries to populate itself by looking for a value in the following places, in this order of priority:

      
  1. ViewData.ModelState [控件名称。Value.RawValue
  2.   传递给HTML辅助方法
  3. 在值参数,如果你已被称为   过载不包括一个值   参数,然后   ViewData.Eval(控件名称)
  4.   
  1. ViewData.ModelState["controlName"].Value.RawValue
  2. value parameter passed to HTML helper method, or if you’ve called an overload that doesn’t include a value parameter, then ViewData.Eval("controlName")

的ModelState是一个临时存储区域   这ASP.NET MVC使用保留   传入试图值加上绑定   和验证错误。请注意,   它是在优先列表的顶部,   因此它的值覆盖任何你   可以明确设置。该公约   意味着你可以传递一个明确的   值参数作为辅助的   默认或初始值;但当   重新描绘后视图   验证失败,助手将   保留所有用户输入值   preference到默认值。

ModelState is a temporary storage area that ASP.NET MVC uses to retain incoming attempted values plus binding and validation errors. Notice that it’s at the top of the priority list, so its values override anything you might set explicitly. This convention means that you can pass an explicit value parameter to act as the helper’s default or initial value; but when rerendering the view after a validation failure, the helper will retain any user-entered value in preference to that default.

我已经验证ViewData.ModelState [页]。Value.RawValue不包含已缠着我意想不到的页码。什么是解决这一问题的最佳方法是什么?

I have verified that ViewData.ModelState["Page"].Value.RawValue does contain the unexpected page number that is pestering me. What's the best way to work around this?

推荐答案

但问题是,ModelState中拿着DropDownList的价值传递。为HtmlHelper的输入控制,ModelState中是在所输入的控制显示一个值时优先于明确设置的值。

The problem was that ModelState was holding the dropdownlist's passed in value. For the HtmlHelper input controls, ModelState is given priority over explicitly set values when displaying a value in the input control.

下面是我做了我的操作方法来解决这个问题:

Here is what I have done in my action method to fix the problem:

ModelState.Remove("Page");
return View(search);

我很乐意听取固定我的问题的其他方式。

I am interested in hearing other ways of fixing my problem.

这篇关于ASP.NET MVC DropDownList的选定值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆