8/6/14

how to set dropdown vLUE IN MVC

@Html.DropDownListFor(model => model.FromAccount, (IEnumerable<SelectListItem>)ViewData["FromAccount"], "Select from Account")

FILL THE VIEW BAG
ViewBag.toAccount = new SelectList(db.GetIEnumerableAccountDetails(userID, "SI"), "AccountID", "AccountName");

      public IEnumerable GetIEnumerableAccountDetails(int userID, string psp)
        {
            SqlConnection con = Getcon();
            SqlCommand cmd = new SqlCommand("GetUserAccount", con);
            cmd.Parameters.AddWithValue("@pSp", psp);
            cmd.Parameters.AddWithValue("@puserID", userID);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt.AsEnumerable().Select(row =>
            {
                return new AccountDetail
                {
                    AccountID = Convert.ToInt32(row["AccountID"]),
                    AccountName = row["AccountName"].ToString()

                };
            });
        }

No comments: