简体   繁体   中英

populating two drop down lists with static list at the page load in mvc

I need to be filled two drop downlists with some static list items at the page load in view for that purpose i have done like this ...

Controller

  public ActionResult Index()
  {           
       GetStatus();
       GetChangeTypeTransactions();
       return View();
  }
  private static TransactionHistoryModel GetStatus()
  {
        var model = new TransactionHistoryModel
        {
            Status= "None",
            StatusList = new[]
            {
                 new SelectListItem { Value = "None", Text = "None" },
                 new SelectListItem { Value = "Success", Text = "Success" },
                 new SelectListItem { Value = "Failed", Text = "Failed" },
                 new SelectListItem { Value = "Queued", Text = "Queued" }
            }
        };
        return model;
    }

    private static TransactionHistoryModel GetChangeTypeTransactions()
    {
        var model = new TransactionHistoryModel
        {
            TypeOfChange = "None",
            TypeOfChangeList = new[]
            {
                new SelectListItem { Value = "None", Text = "None" },
                new SelectListItem { Text = "Delete Item", Value = "Delete Item" },
                new SelectListItem { Text = "Delete Vendor", Value = "Delete Vendor" },
                new SelectListItem { Text = "Delete Member", Value = "Delete Member" },
                new SelectListItem { Text = "Update CostPage Header", Value = "Update Cost Page Header" },
                new SelectListItem { Text = "Update Item", Value = "Update Item" }
            }
        };
        return model;                              
    }

View

 <div class="firstTxtBox">
       @Html.DropDownListFor(m=>m.Status, Model.StatusList,new { style = "width:120px" })  
    </div>
    <div class="SecondLbl">
        @Html.Label("Type Of Change")
    </div>
     <div class="SecondTxtBox">
        @Html.DropDownListFor(m=>m.TypeOfChange, Model.TypeOfChangeList, new { style = "width:120px" })
     </div>

Model

public class TransactionHistoryModel
{
    public string Status { get; set; }
    public IEnumerable<SelectListItem> StatusList { get; set; }
    public string TypeOfChange { get; set; }
    public IEnumerable<SelectListItem> TypeOfChangeList { get; set; }
}

but I am getting NullReference Exception at the starting itself at this line :

 @Html.DropDownListFor(m=>m.Status, Model.StatusList,new { style = "width:120px" })  

I need to get items to be filled in two dropdown lists at page load it self but I am gettinq error, Would any have any idea and any suggestions on this .... I am using MVC 4 version

Many Thanks in Advance...

Try this,

Controller

 public ActionResult Index()
  {     
          TransactionHistoryModel model = new TransactionHistoryModel();      
           model.StatusList = GetStatus();
            model.TypeOfChangeList = GetChangeTypeTransactions();
OR
        ViewBag.statusasdID = GetStatus();
        ViewBag.TypeofasdID = GetChangeTypeTransactions();
           return View(model);
      }

View

@Html.DropDownListFor(m => m.statusid, Model.StatusList , "--Select--")

@Html.DropDownListFor(m => m.TypeofId, Model.TypeOfChangeList , "--Select--")

OR
   @Html.DropDownList("statusid", (SelectList)ViewBag.statusasdID , "--Select--")
   @Html.DropDownList("TypeofId", (SelectList)ViewBag.TypeofasdID , "--Select--")

Model

    public class TransactionHistoryModel
    {
        public int statusid { get; set; }
        public int TypeofId { get; set; }
        public string Status { get; set; }
        public IEnumerable<SelectListItem> StatusList { get; set; }
        public string TypeOfChange { get; set; }
        public IEnumerable<SelectListItem> TypeOfChangeList { get; set; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM