Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

asp.net mvc - Getting form values with MvcPager

When I select any page of my PagedList<T> the [HttpGet]method is activated. Because of this, Im not able to use Request.Form.GetValues().

I just saw the Requestvariable into the immediate window and realized that I dont see any value that I want from the IDs that I have ( the request dont bring me any value of any ID of the screen, since Im not using the post method).

I got a grid (table) and one field of each record is a checkbox, after the pagination, I need know wich checkbox is enabled to keep it checked ( I already made the code to keep it checked, I just need to know how Im going to see if those checkboxes are checked.

I already know their ID too.

Im using the following control to generate those checkboxes:

//foreachloop 
<%: Html.CheckBox(item.ID.ToString(), item.isChecked)%>

For my pagination, Im using the following:

Aspx: <%: Html.Pager(Model.ListaGridPaginada)%>

This pager is a overload that calls the following:

public static MvcHtmlString Pager(this HtmlHelper helper, int totalPageCount, int pageIndex, string actionName, string controllerName, 
        PagerOptions pagerOptions, string routeName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
    {
        var builder = new PagerBuilder
            (
                helper,
                actionName,
                controllerName,
                totalPageCount,
                pageIndex,
                pagerOptions,
                routeName,
                routeValues,
                htmlAttributes
            );
        return builder.RenderPager();
    }

I call the method wich constructs this variable with the following parameters:

public static MvcHtmlString Pager<T>(this HtmlHelper helper, PagedList<T> pagedList)
    {
        if (pagedList == null)
            return Pager(helper,null, null);
        return Pager(helper, pagedList.TotalPageCount, pagedList.CurrentPageIndex, null, null, null,null, null,null);
    }

This pagerHelper is from Webdiyer

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I think if you wrap it in a form and you set the method to GET, then it should pass in all of your form values into the query string. Something like this:

using (Html.BeginForm("Controller", "Action", FormMethod.Get))
{
    // grid in here
    //foreachloop 
    <%: Html.CheckBox(item.ID.ToString(), item.isChecked)%>
    <input type="submit" value="Save" />
}

When you click the submit button, your query string will look something like this:

Action?ID1=false&ID2=true&ID2=false

Which, you'll look at the query string to determine if the checkbox ID contains true. See how ID1=false, but ID2 is true and false? That means ID1 was unchecked, but ID2 was checked.

Request.QueryString["ID2"].Contains("True")

If that doesn't work for you, you could keep track of which checkboxes are checked with javascript by putting their checked state in a hidden value and passing that around.

Does that help any?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...