mercredi 5 août 2015

How to set selected drop down in mvc

I have Album table which has PhotoAlbum_ID and have Video table which also have Video_AlbumID as foreign key.

At the time of edit record in Video table how to set selected dorp down item?

All the available album names populate in drop down.

Controller

BindAlbumList will bind all the available albums in database.

private void BindAlbumList()
{
    using (ShreejidhamApplicationEntities objConnection = new ShreejidhamApplicationEntities())
    {
        ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));
        ObjectResult<tbl_PhotoAlbum_SelectAll_Result> model;
        model = objConnection.tbl_PhotoAlbum_SelectAll(objIErrorCode);
        List<PhotoAlbum> list = new List<PhotoAlbum>();
        if (Convert.ToInt32(objIErrorCode.Value) == 0)
        {
            list = model.Select(x => new PhotoAlbum
            {
                PhotoAlbum_ID = x.PhotoAlbum_ID,
                PhotoAlbum_Name = x.PhotoAlbum_Name
            }).ToList();
            PhotoAlbum objAlbum = new PhotoAlbum();
            objAlbum.PhotoAlbum_ID = 0;
            objAlbum.PhotoAlbum_Name = "--Select--";
            list.Insert(0, objAlbum);
        }
        ViewBag.AlbumDDL = new SelectList(list, "PhotoAlbum_ID", "PhotoAlbum_Name");
    }
}

    public ActionResult Edit(int? id)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (ShreejidhamApplicationEntities objConnection = new ShreejidhamApplicationEntities())
                {
                    BindAlbumList();
                    string sErrorCode = string.Empty; string sVideoID = string.Empty;

                    ObjectParameter objIErrorCode = new ObjectParameter("ErrorCode", typeof(Int32));

                    ObjectResult<TBL_VIDEO_SelectOne_Result> model;
                    model = objConnection.TBL_VIDEO_SelectOne(id, objIErrorCode);

                    if (Convert.ToInt32(objIErrorCode.Value) == 0)
                    {
                        var Video = model.Select(x => new VideoViewModel()
                        {
                            Video_ID = x.Video_ID,
                            Video_Path = x.Video_Path,

                        }).FirstOrDefault();

                        return View(Video);
                    }
                    else
                    {
                        ModelState.AddModelError("", "Ooops! Something went wrong...");
                        return View();
                    }
                }
            }
            return View();
        }
        catch (Exception Ex)
        {
            ModelState.AddModelError("", Ex.Message);
            return View();
        }
    }

View

 <div class="form-group">
      @Html.LabelFor(model => model.tbl_PhotoAlbum.PhotoAlbum_Name, new { @class = "control-label col-sm-2" })
      <div class="col-sm-3">
           @Html.DropDownListFor(model => model.tbl_PhotoAlbum.PhotoAlbum_Name, (SelectList)ViewBag.AlbumDDL, new { @class = "form-control" })
           @Html.ValidationMessageFor(model => model.tbl_PhotoAlbum.PhotoAlbum_Name)
      </div>
 </div>

Aucun commentaire:

Enregistrer un commentaire