question

MJ-8053 avatar image
0 Votes"
MJ-8053 asked AgaveJoe edited

dropdown marking selected value when it should not be

Some background. A user takes a personality assessment based on a job that he/she is applying for. The Managers of the company can then go into the system and pull up that report. Scoring of this is done on the stanine system (standard nine bell curve). Every job has their own benchmark pattern that is displayed in the report (a benchmark pattern is the range of stanines within a particular dimension that are ideal for the candidate to score within). The report is accessed by clicking a link displayed as a button on the page.

Our customer would like for us to provide an option for the viewing manager to view the report using other benchmark patterns. So I have created a form on the page where the manager can select different jobs via a drop down. My issue is that the dropdown is marking as selected the job the applicant took the assessment for and i do not want that to be the case.

I thought that I needed to provide the selected value in dropdowns

Here is the code
//Models used

 public class Applicant
 {
     public string ApplicationId { get; set; }
     public string UserId { get; set; }
     public string Email { get; set; }
     public string PhoneNumber { get; set; }
     public int JobId { get; set; }
     public int LocationId { get; set; }
     public DateTime DateTaken { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string JobTitle { get; set; }
     public string City { get; set; }
     public int Completed { get; set; }  
     public bool Finished { get; set; }
     public bool Viewed { get; set; }
 }
    
 public class Jobs
 {
     public string JobTitle { get; set; }
     public int JobId { get; set; }
     public bool LimitedViewing { get; set; }
     public int AssessmentID { get; set; }
     public bool Active { get; set; }
     public bool Archive { get; set; }
     public bool HasLocations { get; set;}
        
     public List<Jobs> JobsList(string UserID)
     {
         using (SqlConnection oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
         {
             using (SqlCommand oCmd = new SqlCommand())
             {
                 try
                 {
                     oCmd.CommandText = "ListJobs";
                     oCmd.CommandType = CommandType.StoredProcedure;
                     oCmd.Parameters.Add("@UserID", SqlDbType.NVarChar).Value = UserID;
                     oCmd.Connection = oConn;
                     oConn.Open();
                     using (SqlDataReader reader = oCmd.ExecuteReader())
                     {
                         List<Jobs> myList = new List<Jobs>();
                         while (reader.Read())
                         {
                             Jobs job = new Jobs
                             {
                                 JobId = Convert.ToInt16(reader[0]),
                                 JobTitle = reader[1].ToString()
                             };
                             myList.Add(job);
                         }
                         return myList;
                     }
                 }
                 finally
                 {
                     oConn.Close();
                     oCmd.Dispose();
                 }
             }
         }
     }
 }
    
 //Controller code
 public ActionResult Details(string ApplicationId)
 {
     try
     {
         if (ApplicationId == "" || ApplicationId == null)
         {
             return RedirectToAction("Index");
         }
         string UserID = System.Web.HttpContext.Current.Session["UserID"] as String;
         ViewData["UserID"] = UserID;
         if (ViewData["UserID"] != null)
         {
             Applicant app = new Applicant();
             var ThisApplicant = app.GetApplicantData(ApplicationId);
             AdminUser admin = new AdminUser();
             ViewBag.AdminEmail = admin.GetAdminUserEmail(UserID);
             ViewBag.UserID = UserID;
             Jobs job = new Jobs();
             ViewBag.Jobs = new SelectList(job.JobsList(UserID), "JobId", "JobTitle");
             return View(ThisApplicant);
         }
         ViewData["ReturnUrl"] = "Applicants/Index";
         return View("LoggedOut");
     }
     catch (Exception ex)
     {
         ErrorOccured(ex);
     }
     return RedirectToAction("Index", "Error", new { Area = "" });
 }
    
 //The form in the view with the dropdown to allow for selecting different jobs
 @using (Html.BeginForm("ViewDifferentJob", "Applicants", FormMethod.Post, new { id = "myForm" }))
 {
     <input type="hidden" name="ApplicationId" value="@Model.ApplicationId" />
     <div class="row">
         <div class="col-xs-6 col-md-3 col-md-offset-2"><label>Job pattern to view</label>@Html.DropDownList("JobId", (IEnumerable<SelectListItem>)ViewBag.Jobs, "Make Selection", htmlAttributes: new { @class = "form-control" })</div>
         <div class="col-xs-6 col-md-3"><label>Report Type</label><select name="SsOnly" class="form-control"><option value="false">Full Report</option><option value="true">Scoresheet Only</option></select></div>
         <div class="col-xs-3 col-md-3"><label>&nbsp;</label><input type="submit" value="View Assessment" class="btn btn-block btn-default btn-assessment" /> </div>
     </div>
 }

How do I make it so the dropdown does NOT mark as selected the job that applicant took the assessment for?

dotnet-aspnet-mvc
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the Html Helper uses the model property (ThisApplicant.JobId) to select an option form the (IEnumerable<SelectListItem>)ViewBag.Jobs. You typically want the UI to match the database record state.

A few options are, do not populate the JobId, set the Id to zero, or change JobId to a nullable type and set the property to null.

Seems like a poor design choice if the manager does not intend to make a change to the record. Perhaps you need to rethink the design???

0 Votes 0 ·

Come again on the poor design choice?? It is designed to allow the user to select a job other than the one the applicant applied for. What would you do differently? Inquiring minds want to know

0 Votes 0 ·

Come again on the poor design choice?? It is designed to allow the user to select a job other than the one the applicant applied for. What would you do differently? Inquiring minds want to know

Usually, details refer to the actual state of a entity. In this case, the job an applicant applied for. Can you explain why this design wants to ignore the user's original selection persisted in the database?

Maybe this is not a detail view but more of a search or report page where the dropdown are part of the filter criteria?



0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

Below is a basic example that exercises the Html helper feature I described above.

 namespace MvcBasic.Controllers
 {
     public class ViewModel
     {
         public int OptionValue { get; set; }
         public int Hello { get; set; }
     }
     public class BasicFormsController : Controller
     {
         // GET: BasicForms
         [HttpGet]
         public ActionResult Index()
         {
             ViewBag.Options = PopulateOptions();
             ViewModel vm = new ViewModel() { OptionValue = 3 };
             return View(vm);
         }
    
         [HttpPost]
         public ActionResult Index(ViewModel model)
         {
             ViewBag.Options = PopulateOptions();
             return View(model);
         }
    
         private List<SelectListItem> PopulateOptions()
         {
             List<SelectListItem> options = new List<SelectListItem>();
    
             for (int i = 1; i < 10; i++)
             {
                 options.Add(new SelectListItem() 
                 {
                     Text = $"Option {i}",
                     Value = $"{i}",
                 });
             }
             return options;
         }
     }
 }

The first DropDownList selects option 3 because the model property OptionValue equals 3 and the DropDownList name parameter is "OptionValue". The second DropDownList defaults to the first item in the list.

 @model MvcBasic.Controllers.ViewModel
 @{
     ViewBag.Title = "Index";
 }
    
 <h2>Index</h2>
    
 <form method="post">
     <div>
         @Html.DropDownList("OptionValue", (List<SelectListItem>)ViewBag.Options)
     </div>
     <div>
         @Html.DropDownList("Hello", (List<SelectListItem>)ViewBag.Options)
     </div>
     <div>
         <input id="Submit1" type="submit" value="submit" />
     </div>
 </form>

Another option is simply...

 <div>
     @Html.DropDownList("Options")
 </div>


Where "Options" matches the ViewBag key.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MJ-8053 avatar image
0 Votes"
MJ-8053 answered

The site is an assessment center for people that already have an applicant tracking system but want to use our assessments and not link to them through our API. To that end the details page contains the name of the Applicant, their email address and phone number along with the application ID for this particular application, the name of the Job, the location applicant was applying to, the date they took the assessment and whether or not it was viewed.

Then it also provides a way for the manager to view the report, archive the application, leave recruiter notes on the applicant, or send recruiter email to another manager that provides links to the report and/or the recruiter notes as well as whatever else they want to put in the email.

Sales department wanted to make it so the manager could look at the benchmark patterns for other jobs.

The dropdown is merely there to provide a way to compare this applicant to other jobs that they may be better suited for. It has absolutely nothing to do with what job this applicant took thje assessment under.

So this dropdown actually has nothing to do with this applicant zero zip nada a big old goose egg. Which is why I did not put in a selectedValue. This is why I was wondering why it would show as selected the job the applicant had taken the assessment on.

I was under the belief when you created the SelectList within the controller that I needed to actually specify in the SelectList a selectedValue , which I did not do.

public SelectList(IEnumerable items, string dataValueField, string dataTextField); #4 of 9 the one I was using
public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue); #5 of 9

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.