2 Replies - 6565 Views - Last Post: 03 December 2012 - 06:46 PM

#1 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

Drop down list value returns to ---select--- after page refresh

Posted 30 November 2012 - 08:45 AM

Heys guys

So in my application the user will select a name from the drop down list, click 'view' and the corresponding values will display on page.

A hyperlink is then used to sort the list in ascending order. For this to happen the page refreshes and displays the new order of the list.

The value of the drop down list returns back to its original value of 'select' instead of remaining the name of the person selected.

My Model:
public class HolidayList
    {
        public List<Holiday> HList4DD { get; set; }
        public List<Person> PList4DD { get; set; }

        public int currentPersonID { get; set; }
        public IEnumerable<SelectListItem> Categories { get; set; }

        public HolidayList()
        {
            HList4DD = new List<Holiday>();
            PList4DD = new List<Person>();
            }
        }
    }

----------------------------------------

my controller:
 [HttpPost]
        public ViewResult Index(int HolidayDate)
        {
            var holidays = db.Holidays.Include("Person");

            HolidayList model = new HolidayList();

            model.currentPersonID = HolidayDate;
            model.PList4DD = db.People.ToList();           
            model.Categories = holidays.Select(x => new SelectListItem
                                            {
                                                Value = x.Id.ToString(),
                                                Text = x.Person.Name
                                            }
                                          );


            int data = HolidayDate;

            model.HList4DD = db.Holidays.Where(h => h.PersonId == HolidayDate).ToList();      

            return View(model);

        }

        [HttpGet]
        public ViewResult Index(string sortOrder, int? currentPersonID)
        {
            var holidays = db.Holidays.Include("Person");
 
            HolidayList model = new HolidayList();
 
            //not null
            if (currentPersonID.HasValue)
            {
                model.currentPersonID = currentPersonID.Value;

            }
            else
            {
                model.currentPersonID = 0;
            }

            model.PList4DD = db.People.ToList();

            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "date" : "";
            var dates = from d in db.Holidays
                        where d.PersonId == currentPersonID.Value
                        select d;
     
            switch (sortOrder)
            {
                case "date":
                    dates = dates.OrderBy(p => p.HolidayDate);
                    break;
            }

            model.HList4DD = dates.ToList();

            return View(model);
        }

-----------------------

my view

i've tried a number of different attempts here, the following code worked but has the drop list problem
@Html.DropDownListFor(model => model.HList4DD.First().HolidayDate,
                                new SelectList(Model.PList4DD, "Id", "Name"),
                               // Model.currentPersonID
                                "---Select---"
                                )  *@



-----------
my attempts to resolve this are:

   @* @Html.DropDownList("HolidayDate", Model.Categories, "---Select---")*@

           @*  @Html.DropDownListFor("HolidayDate", x => x.HolidayDate, Model.Categories)
                              *@


------------

Any help much appreciated

thaks

Is This A Good Question/Topic? 0
  • +

Replies To: Drop down list value returns to ---select--- after page refresh

#2 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

Re: Drop down list value returns to ---select--- after page refresh

Posted 03 December 2012 - 03:30 AM

any suggestions guys?

Thanks
J
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Drop down list value returns to ---select--- after page refresh

Posted 03 December 2012 - 06:46 PM

if the list isn't changing, other than the order, is there a reason you're not trying to do this on the client side using javascript instead of making a call to the server?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1