Wednesday, 23 April 2014

DropdownList bind and get detail





DropdownList bind and get detail


Calling Statement:

 ViewBag.CategoryDropDownList= categoryBind(0);



Method:

public List<SelectListItem> categoryBind(int CategoryId)
        {
            List<SelectListItem> CategoryDropDownList = new List<SelectListItem>();
            List<Category> CategoryList = new PostRepository().GetAllCategory();
            foreach (var item in CategoryList)
            {
                if (item.CatId == CategoryId)
                {
                    CategoryDropDownList.Add(new SelectListItem { Text = item.CatName, Value = Convert.ToString(item.CatId), Selected=true});
                }
                else
                {
                    CategoryDropDownList.Add(new SelectListItem { Text = item.CatName, Value = Convert.ToString(item.CatId)});
                }
            }
            return CategoryDropDownList;
        }

View Side:
<script>
        var jqListUser = jQuery.noConflict();
        jqListUser(document).ready(function () {
            jqListUser('#CategoryDropDownList').change(function () {               
                list = jqListUser1(this);
                var listvalue = list.val();
                document.getElementById("dropdownselectedid").click();
            });
        });
</script>
@using (Html.BeginForm("ListUserPictures", "Picture", FormMethod.Post, new { id = "formUserpicture" }))
 {
@Html.DropDownList("CategoryDropDownList")
   <input type="submit" id="dropdownselectedid" name="dropdownselectedid"   style="display:none" />
}
Fetching Data(Post Method):
CategoryId = Convert.ToInt32(Request.Form["CategoryDropDownList"]);

No comments:

Post a Comment