Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

c# - ASP.NET MVC DropDownList Selected Value Problem

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

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.

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.

Here is the action method:

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

Please help.

EDIT

Page 333 of Pro ASP.NET MVC Framework by Steven Sanderson:

How Input Controls Get Their Values

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["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 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...