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.