如何在ASP.net MVC中使用Postback

时间:2014-05-20 08:29:31

标签: html5 asp.net-mvc-4 razor-2

  How to preserve the selected value after postback for that dropdownlist?

我在我的应用程序中使用了两个下拉列表。我需要根据下拉列表的选定值搜索gridview数据。搜索到的数据以分页列表格式显示。但是,当我移动到pagedlist的下一页时,它显示整个网格视图数据而不是搜索数据。所以任何帮助将不胜感激。

My Model

 namespace CCIOfficeServiceManagementSystem.Models   
 {
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; }
}

}

  My View


  @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>



 My controller's action Method


 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)
         {

         }
         //IEnumerable<SelectListItem> MonthItems = (from m in SelectMonthList
         //                                          select new SelectListItem()
         //                                          {
         //                                              Value = m.MonthSelectedId.ToString(),
         //                                              Text = m.MonthName,


         //                                          });
         //ViewBag.SelectMonthList = MonthItems;
         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));
        //}
        //if (list.Count > 0)
        //{
        //    var airteldetails = from _model in list
        //                        select _model;
        //    return View(airteldetails.ToPagedList(pageNumber,pageSize));
        //}
        //else
        //{
        //    ModelState.AddModelError("Error", "No Data found in Database");
        //    return RedirectToAction("ImportExcelFile", "AirtelManagement");
        //}
    }

0 个答案:

没有答案