0

I Have MVC Razor view with DropdownListFor. I wanna set a default value to that DropdownListFor. I have bind dropdown for using viewbag. and in the viewbag come list and in the list store one Flag property. now i want Flag is 1 then this value I wanna set in the dropdown list. always in the list Flage = 1 is only one object come and other object flag = 0.

This is my view =>

@{ var CategoryList = ViewBag.Category as IEnumerable<ABCBAL.CategoryBAL>; }
                @Html.DropDownListFor(model => model.CatId, new SelectList(CategoryList , "CatId", "Category"), new { @class = "form-control", @id = "ddlCat"})

This my controller method =>

public ActionResult EditCat(int UserId)
{            
    Users Userobj = new Users();          
    ViewBag.Category = Userobj.GetAllcategoryById(UserId).Data;                         
    return PartialView(Cat);
}

This is my User.cs class Method =>

public Status GetAllcategoryById(int UserId)
    {
            Status Status = new Status();         
            DataSet ds = DataAccess.ExecuteDataset(Settings.ConnectionString(), "GetAllcategoryById", UserId);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<CategoryBAL> CatList = new List<CategoryBAL>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    CategoryBAL objCat = new CategoryBAL();

                    objCat.CatId = Convert.ToInt32(ds.Tables[0].Rows[i]["CatId"]);
                    objCat.Category = ds.Tables[0].Rows[i]["Category"].ToString();                        
                    objCat.Falge = Convert.ToInt32(ds.Tables[0].Rows[i]["Falge"]);

                    CatList.Add(objCat);
                }

                Status.Result = true;
                Status.Data = CatList;
            }
            else
            {
                Status.Result = false;
            }            
        return Status;
    }

This is my code and here one flage is come 1 then i want to flage is 1 then default selected in the drop down.

Tejinder Singh
  • 1,070
  • 2
  • 8
  • 24
Edit
  • 385
  • 4
  • 24
  • @Sibi Raj do you have idea how can fix it? – Edit Aug 03 '17 at 06:46
  • show your class code – Tejinder Singh Aug 03 '17 at 06:47
  • Have you see this: https://stackoverflow.com/questions/23799091/html-dropdownlistfor-how-to-set-default-value? The issue is similar to that existing post. – Tetsuya Yamamoto Aug 03 '17 at 06:47
  • @TejinderSingh you mean you want see my method how can i bind viewbag? – Edit Aug 03 '17 at 06:48
  • @Edit yes. show your code – Tejinder Singh Aug 03 '17 at 06:49
  • @TejinderSingh i have edit please see and help me – Edit Aug 03 '17 at 06:55
  • Its the value of `CatId` that determines what is selected. If it matched one of the options values, then that option will be selected. Set the value of `CatId` in the GET method before you pass the model to the view. –  Aug 03 '17 at 06:59
  • @StephenMuecke sorry i can not understand what you want to say can you please explain – Edit Aug 03 '17 at 07:03
  • Just set the value of `CatId` in the model before you pass it to the view. Its impossible to understand your code when it does not even compile. (you have `return PartialView(Cat);` but `Cat` is not even declared!) –  Aug 03 '17 at 07:07
  • @StephenMuecke thanks i have fix it for your information is good. i have pass catid in the view then dafult value is selected – Edit Aug 03 '17 at 07:25

1 Answers1

0

You can set the value from your model like this:

 @Html.DropDownListFor(model => model.CatId, new SelectList(CategoryList , "CatId", "Category"), new { @class = "form-control", @id = "ddlCat",**@value = Model.CatId**})
  • 1
    Absolutely not. Never set the `value` attribute when using a `HtmlHelper` method. –  Aug 03 '17 at 06:58