0

I have several textboxes and one dropdownlist like:

       for (int i = 0; i < count; i++)
       {
        <tr>
        <td>@Html.DropDownListFor(m => m.GetTimeSheetDetails[i].PROJ_ID, (SelectList)ViewBag.ProjectList, "-- Choose a Project --", new { @id = "ddlProjectvalue" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SUN_HRS, new { style = "width:50px; height:30px;", @class = "sunhrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].MON_HRS, new { style = "width:50px; height:30px;", @class = "monhrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].TUE_HRS, new { style = "width:50px; height:30px;", @class = "tuehrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].WED_HRS, new { style = "width:50px; height:30px;", @class = "wedhrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].THU_HRS, new { style = "width:50px; height:30px;", @class = "thurhrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].FRI_HRS, new { style = "width:50px; height:30px;", @class = "frihrs" })
        </td>
        <td>@Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SAT_HRS, new { style = "width:50px; height:30px;", @class = "sathrs" })
        </td>
        </tr>
        </td>
        }

and I want to bind data from database to all the fields , every thing is displaying data perfectly, but dropdown list for proj_id is not showing text even though i am passing value to dropdownlist. i am passing like :

public int GetTimsheetData(int empid, TimesheetModel TimesheetModel)
{
    // GetimeSheet all the rows according employee name
    var emps = (from n in db.TIMESHEETs
                where n.RES_ID == empid
                select n).ToList();
    int count = emps.Count();
    HttpContext.Current.Session["count"] = count;
    try
    {
        List<TimesheetModel> emptyList = new List<TimesheetModel>();
        TimesheetModel.GetTimeSheetDetails = emptyList; //taking Empty List and bind to GetTimesheetDetails for Add items into it.


        //if Employee Name has more than one record.
        if (emps.Count() > 0)
        {
            foreach (var timeSheet in emps)
            {
                TimesheetModel item = new TimesheetModel();
                item.WEEK_CAL_ID = timeSheet.WEEK_CAL_ID;
                item.PROJ_ID = timeSheet.PROJ_ID;
                item.SUN_HRS = timeSheet.SUN_HRS;
                item.MON_HRS = timeSheet.MON_HRS;
                item.TUE_HRS = timeSheet.TUE_HRS;
                item.WED_HRS = timeSheet.WED_HRS;
                item.THU_HRS = timeSheet.THU_HRS;
                item.FRI_HRS = timeSheet.FRI_HRS;
                item.SAT_HRS = timeSheet.SAT_HRS;
                TimesheetModel.GetTimeSheetDetails.Add(item);

            }

        }

    }
    catch (Exception ex)
    {
        throw ex;
    }
    return count;
}

and returning to controller like :

public ActionResult GetEmployeeDetails(int empId, string btn, TimesheetModel timesheetModel)
{
    Employer_BL employerBL = new Employer_BL();
    ViewBag.ProjectList = timesheetModel.getProjects;

    //If GetTimesheetData returns morethan one record 
    if (employerBL.GetTimsheetData(empId, timesheetModel) >= 0)
    {
        timesheetModel.EMP_ID = empId;

        //passes model data to View 
        return View("Timesheet", timesheetModel);
    }
    TimesheetModel model = new TimesheetModel();
    model.EMP_ID = empId;
    return View("Timesheet", model);
}

Where am I doing wrong, dropdownlist showing initial index instead of showing text of passing values. Please help me anyone.

in Separate Class I have written like below to get project names:

    public SelectList getProjects()
    {
        IEnumerable<SelectListItem> projectslist = (from proj in res.PROJECTs where proj.IS_DELETED == "N" select proj).AsEnumerable().Select(projt => new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() });
    return new SelectList(projectslist, "Value", "Text", PROJ_ID);
    }

3 Answers 3

1

It depends on the ViewBag.ProjectList which I cannot found on your source code. You could populate it with an object of type IEnumerable<SelectListItem> with one of the item Selected properties set to true.

public IEnumerable<SelectListItem> GetList()
{
    return (from proj in res.PROJECTs where proj.IS_DELETED == "N" select proj).AsEnumerable().Select(projt => new SelectListItem() { Text = projt.NAME, Value = projt.ID.ToString() }).ToList();
}

on your controller

ViewBag.ProjectList = GetList();

on your view

@{
    var projectList = 
        new SelectList(ViewBag.ProjectList, "Value", "Text", Model.GetTimeSheetDetails[i].PROJ_ID.ToString())
}
@Html.DropDownListFor(m => m.GetTimeSheetDetails[i].PROJ_ID, projectList, "-- Choose a Project --")
Sign up to request clarification or add additional context in comments.

2 Comments

I have Edited and Added Code to get project names, please check it.
You didn't take the result to the ViewBag in your controller.
0

You can try like this method:

[NonAction]
private IEnumerable<SelectListItem> GetData()
{
    return new List<SelectListItem>()
    {
        new SelectListItem(){ Text="--Select--", Value="0"},
        new SelectListItem(){ Text="A", Value="1"},
        new SelectListItem(){ Text="B", Value="2"},
        new SelectListItem(){ Text="C", Value="3"},
    };
}

Call this function in Action Method
public ActionResult Create()
{
    ViewData["categories"] = GetData();
    return View();
}


On your html page:
  <%= Html.DropDownList("cat", (IEnumerable<SelectListItem>)ViewData["categories"])%>

1 Comment

Thanks for Clue, but i don't want to bind all the data, i want to assign value to dropdownlist to show the text, because i have already bind data,i want to get value when i returning with empid and depends on the value it show text @Shashank
0

You can use viewbag . in your controller you can read your data from the database :

public ActionResult Create()
{
  ViewBag.ClassName = new SelectList(objclassrep.GetClasslist(), "Id", "ClassName");
}

And in your view model you can read the data from controller like this :

<div class="editor-label">
            @Html.LabelFor(model => model.ClassId)
</div>
<div class="editor-field">
            @Html.DropDownListFor(x => x.ClassId, (SelectList)ViewBag.ClassName);
            @Html.ValidationMessageFor(model => model.ClassId)
</div>

This code automatically binds ids of your data to DDL Here is class id.

This is th getClassList function :

 public List<Class> GetClasslist()
        {
            return _dbcontext.Classes.ToList();
        }

3 Comments

Thanks for Clue, But my dropdown list contains List like GetTimesheetDetails[index].Proj_Id, i need to pass value to this dropdownlist, i am passing like above, but it is not working.. @Iti Tyagi.
As you can see in my answer i pass the value to DDL here is class Id.do you type the name of VIEWBAG correctly?
viewbag works ,but i need to pass value in the form of list, that list values should bind to dropown list , that shows text with respect to value, i have passed values in the form of list, but not working. @ E A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.