1

I'm selecting the page numbers using html dropdown .After selecting then loading the items.But current selected index of the dropdown not loading in drop down.

<span id="filterRecords">Number of Results Per Page
                   <%:Html.DropDownList("filterByPageNumber", new[]{

                     new SelectListItem { Text = "10", Value = "10" }, 
                     new SelectListItem { Text = "20", Value = "20" },
                     new SelectListItem { Text = "30", Value = "30" }, 
                     new SelectListItem { Text = "40", Value = "40" },
                     new SelectListItem { Text = "50", Value = "50" }
                    },
                     null,
                     new { onchange = "GetResultsPerPage()" })

                 %>.

function GetResultsPerPage() {

        var id = $("#filterByPageNumber option:selected").val();
        $("#recordsperpage").val(id);
          SubmitForm();

    }

eidt:

 <input type="hidden" name="recordsperpage" id="recordsperpage" value="10" />

Please tell me selected index not showing after submitform().if i select 20 or 30 or 40 in dropdown always showing 10only(default) value.please tell me.

user3106578
  • 177
  • 1
  • 2
  • 12
  • can you show the result html from the DropDownList as well as the hidden? input field 'recordsperpage'? Also do you have other code calling GetResultsPerPage or modifying the 'recordsperpage' field? – Per Hornshøj-Schierbeck Dec 16 '13 at 08:36
  • only in dropdown onpage() calling GetResultsPerPage . – user3106578 Dec 16 '13 at 08:39
  • You ask why the selected index is not showing *after* submitform? You mean after postback? There is nothing setting your filterByPageNumber selectedindex so it will always hold the default (first item). Not sure what you mean - Does recordsperpage hold the correct value on postback or is that not working either? – Per Hornshøj-Schierbeck Dec 16 '13 at 08:41
  • Recordsperpage holding the correct value on postback but selectedindex was not showing properly .I tried with selectedindex of dropdown property no use.only showing default 10 value. document.getElementsByName('filterByPageNumber')[0].value = this.value; – user3106578 Dec 16 '13 at 08:44
  • I need to show proper selected index which item i selected in drop down . – user3106578 Dec 16 '13 at 08:46

1 Answers1

0

Check this link out

http://agilewarrior.wordpress.com/2012/12/13/how-to-simple-html-dropdownlistfor-mvc-net/

As you can see, you need to set a parameter to set the selected index. Hope that helps, if not let me know and i'll provide example :)

There are also multiple questions already on stackoverflow about this, check this for example: ASP.NET MVC Html.DropDownList SelectedValue

You could do something like this

<%:Html.DropDownList("filterByPageNumber", Model.ResultPerPage,
                     null,
                     new { onchange = "GetResultsPerPage()" })

Your model would then have a property - a list of SelectListItem and you set (server-side) .SelectedItem = true on the SelectListItem that matches the value of your recordsperpage

server-side

var resultsPerPage = List<SelectListItem>() { new SelectListItem { Text = "10", Value = "10" }, 
                     new SelectListItem { Text = "20", Value = "20" },
                     new SelectListItem { Text = "30", Value = "30" }, 
                     new SelectListItem { Text = "40", Value = "40" },
                     new SelectListItem { Text = "50", Value = "50" }}
resultsPerPage.Single(r => r.Value == recordsperpage).Selected = true; 
viewModel.ResultPerPage = resultsPerPage;

I'm sure you can clean it up, but this provides a simple example that should get you going.

Community
  • 1
  • 1
Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
  • please provide example i am not able to open that wrodpress.com. – user3106578 Dec 16 '13 at 09:09
  • I thought my last link (not wordpress but stackoverflow) would provide the example needed? You really should not do the SelectListItem array in the html but rather pass it as part of the model. In that model you set .SelectedItem = true on the item that was selected. – Per Hornshøj-Schierbeck Dec 16 '13 at 09:16
  • I changed the link - i noticed it linked to a less relevant question/answer. Check the new one – Per Hornshøj-Schierbeck Dec 16 '13 at 09:18
  • i tried above code getting error in selecteditem "System.web.mvc.selectedlistitem does not a definition for selectedItem .conatin.resultsPerPage.Single(r => r.Value == recordsperpage).SelectedItem = true; – user3106578 Dec 16 '13 at 09:56
  • Sorry the property is called .Selected and not .SelectedItem. I updated my answer to reflect it – Per Hornshøj-Schierbeck Dec 16 '13 at 10:14