3

I am creating a Trailer for existing Driver (that can be selected from Drop Down list).

@Html.DropDownListFor(x => x.Driver.driverID, (SelectList)ViewBag.DriverID, "-- Please Select -- ", new { @class = "form-control" })

For CREATE function it works perfectly.

//Create Get
public ActionResult Create()
{
    ViewBag.DriverID = new SelectList(db.Drivers, "driverID", "driverFullName");
    return View();
}

For EDIT function (edit trailers number and leave the driver NULL) it does not work.

//Edit Get
public ActionResult Edit(int? id)
{
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     Trailer trailer = db.Trailers.Find(id);
     if (trailer == null)
     {
         return HttpNotFound();
     }
     ViewBag.DriverID = new SelectList(db.Drivers.ToList(), "driverID", "driverFullName");
     return View(trailer);
}    

I have the -- Please Select -- on the drop down list as a first empty value.
How could I put a NULL value on this first empty value (so the trailer would have NO driver selected) from drop down list?

11
  • Please show us Create and Edit actions in your controller and how you populate ViewBag.DriverID. Commented Mar 6, 2017 at 17:49
  • why you want null? Commented Mar 6, 2017 at 18:03
  • If --Please Select-- was selected that means its NULL right? Why you want null? Commented Mar 6, 2017 at 18:04
  • --Please Select-- is just an empty slot. I want a NULL for the trailer none driver be selected. Commented Mar 6, 2017 at 18:27
  • can you provide Drivers model and you want to select --Please Select-- will will return null ? but you cant submit when its --Please Select-- right? Commented Mar 6, 2017 at 18:28

3 Answers 3

4

this is because your driverID property in Drivers class is not nullable ? that is why when you select -- Please Select -- it gives validation message The driverID field is required so you should set driverID to nullable like

public int? driverID {get;set;}

now its default value will be null when you select -- Please Select --

Edit

another way is to add a default object manually like

in view

 @Html.DropDownListFor(x => x.Driver.driverID, (SelectList)ViewBag.DriverID, new { @class = "form-control" })

and in edit action

public ActionResult Edit(int? id)
{
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     Trailer trailer = db.Trailers.Find(id);
     if (trailer == null)
     {
         return HttpNotFound();
     }

     var list = db.Drivers.ToList();
     list.Insert(0, new Drivers() {driverFullName = "-- Please Select --"});
     ViewBag.DriverID = new SelectList(list, "driverID", "driverFullName"); //showing the list of drivers on edit page
     return View(trailer);
}
Sign up to request clarification or add additional context in comments.

11 Comments

This is my Trailer Model class. I have it int? public class Trailer { public int trailerID { get; set; } public int? DriverID { get; set; } public virtual Driver Driver { get; set; } }
It was like this from the beginning. I had it from before. It doesnot help
you are using Driver.driverID in code so it should be in driver class
but driverID in Driver class is a primary key
are you using view model or domain model?
|
1

When using the Tag Helpers in ASP.NET Core the following snippet might help:

<select asp-for="DriverId" asp-items="DriverIdList" class="form-control">
  <option value="">-- please select --</option>
</select>

Note the value="", without this attribute modelbinding will fail to bind to a nullable int.

Comments

0

You can also use the asp-for tag helper with a SelectListItem list

Then this is what you write in your form in the view

<select asp-for="(Whatever the value is associated to)" asp-items="The List<SelectListItem> list">
    <option disabled selected>---Select---</option> 
<!-- This line will go to the top of the dropdown list -->
</select>

This will create a dropdown list where by default nothing is selected and this default value will be null

You can also refer to this other post on stack overflow:

Set Default/Null Value with Select TagHelper

Comments

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.