将所选的dropdownlist值与PagedList的Model绑定

时间:2014-05-27 09:13:46

标签: asp.net-mvc razor-2 pagedlist

任何人都可以建议我如何将下拉列表的选定值与模型绑定,以便当我重定向到分页列表的下一页时,所选值应该保持不变。

This is My Model.


 public class AirtelManagementModel
{
    public long MobileAcNumber { get; set; } 
    public long AirtelNumber { get; set; }
    public int OneTime { get; set; }
    public float MonthlyCharges { get; set; }
    public float CallCharges { get; set; }
    public float ValueAddedServices { get; set; } 
    public float MobileInternetUsage { get; set; }  
    public float Roaming{ get; set; } 
    public float Discounts { get; set; }
    public float Taxes { get; set; } 
    public float TotalCharges { get; set; }
    public string WhoUploaded { get; set; }
    public DateTime UploadedDate { get; set; }
    public DateTime DateOfCreation { get; set; }
    public int ImportDateId { get; set; }


    public List<MonthListClass> MonthList
    { 
        get;
        set;
    }



    public List<clsYearOfDate> YearList
    {
        get;
        set;
    }
}

public class MonthListClass
{
    public int MonthSelectedId { get; set; }
    public string MonthName { get; set; }
}


public class clsYearOfDate
{
    public int YearSelectedId { get; set; }
    public string YearOfDate { get; set; }
}

这是我的控制器&#39;行动方法

   public ActionResult ViewDataOfDatabase(string sortorder, string currentFilter, string searchString, int? page,FormCollection collection) 
     {
         CCIRepository _repository = CCIRepository.CreateRepository();
         AirtelManagementModel _Airtelmodel = new AirtelManagementModel();
         IEnumerable<CityListClass> CityList = _repository.GetCities();
         IEnumerable<SelectListItem> CityListItems = from c in CityList
                                                     select new SelectListItem()
                                                     {
                                                         Value = c.CityName.ToString(),
                                                         Text = c.CityName.ToString(),
                                                         Selected = c.CityName == Request["CityNames"],
                                                     };
         ViewBag.CityList = CityListItems;
         IEnumerable<clsYearOfDate> SelectList = GetYears();
         //IEnumerable<MonthListClass> SelectMonthList = GetMonths(YearId);
         IEnumerable<SelectListItem> Yearitems = (from v in SelectList
                                             select new SelectListItem()
                                             {
                                                 Value = v.YearSelectedId.ToString(),
                                                 Text = v.YearOfDate.ToString(),
                                                 Selected = v.YearOfDate == Request["Yearitems"],
                                             });

         ViewBag.SelectList = Yearitems;
         int DateId=0;
         string CityName = string.Empty; 
         try 
         { 

                int SelectedYear = Convert.ToInt16(collection["Yearitems"].ToString());
                int SelectedMonth = Convert.ToInt16(collection["MonthItems"].ToString());
                CityName = collection["CityNames"].ToString();
                DateId = _repository.GetImportDateId(SelectedYear, SelectedMonth);


         }
         catch(NullReferenceException Ex)
         {
             Console.WriteLine(Ex);
         }
 IEnumerable<SelectListItem> MonthItems = Enumerable.Empty<SelectListItem>();
         ViewBag.SelectMonthList = MonthItems;
        List<AirtelManagementModel> list = ViewDetails();
        ViewBag.CurrentSort = sortorder;

        ViewBag.PhoneSortParm = String.IsNullOrEmpty(sortorder) ? "Phone_desc" : "";
        if (searchString != null )
        {
            page = 1;
        }
        else
        {

            searchString = currentFilter;
        }
        //if(searchString!=null)
        //{

            ViewBag.CurrentFilter = searchString;
            var airteldetails = from _model in list
                                select _model;
            if(!String.IsNullOrEmpty(searchString) && DateId!=0 && !String.IsNullOrEmpty(CityName))
            {
                airteldetails = _repository.FilterAirtelDetails(searchString, DateId, CityName);
                int PageSize = 5;
                int PageNumber = (page ?? 1);
                return View(airteldetails.ToPagedList(PageNumber, PageSize));
            }
            //airteldetails=airteldetails.OrderByDescending(A=>A.AirtelNumber);
            int pageSize = 5;
            int pageNumber = (page ?? 1); 
            //return View(airteldetails.ToList());
            return View(airteldetails.ToPagedList(pageNumber, pageSize));

}

这是我的观点

    @using (Html.BeginForm("ViewDataOfDatabase", "AirtelManagement",FormMethod.Post))
{
<h3>Search by PhoneNumber:@Html.TextBox("SearchString",ViewBag.CurrentFilter as string)</h3>


    <p><h3>Year:@Html.DropDownList("Yearitems", (IEnumerable<SelectListItem>)ViewBag.SelectList, "Select Year")</h3>
    <h3>Month:@Html.DropDownList("MonthItems", (IEnumerable<SelectListItem>)ViewBag.SelectMonthList, "Select Month")</h3>
    <h3>City: @Html.DropDownList("CityNames", (IEnumerable<SelectListItem>)ViewBag.CityList, "Select City")</h3></p>

<p><input type="submit" value="Search" /></p>

<script>
    $(document).ready(function () {
        $("#Yearitems").change(function () {
            //debugger;
            //alert($("#Yearitems>option:selected").attr("Value"));
            $.ajax({
                type: "Post",
                url: '@Url.Action("GetMonths","AirtelManagement")',
                data: { YearId: $("#Yearitems>option:selected").attr("Value") },
                datatype: "Json",
                success: function (data) {
                    //debugger;
                    $("#MonthItems").html("");
                    $.each(data, function (index, item) {
                        $("#MonthItems").append(new Option(item.MonthName, item.MonthSelectedId));
                    });
                },
                error: function () {
                    alert("Select Year");
                }
            });
        });
    });
</script>

1 个答案:

答案 0 :(得分:0)

根据Post

中给出的答案
@{
   List<SelectListItem> listItems= new List<SelectListItem>();
   listItems.Add(new SelectListItem
        {
          Text = "Exemplo1",
          Value = "Exemplo1"
        });
   listItems.Add(new SelectListItem
        {
            Text = "Exemplo2",
            Value = "Exemplo2",
            Selected = true
        });
   listItems.Add(new SelectListItem
        {
            Text = "Exemplo3",
            Value = "Exemplo3"
        });
}

@Html.DropDownListFor(model => model.tipo, listItems, "-- Select Status --")