Sidarth
Sidarth

Reputation: 136

First element of the DropdownList not getting selected

I have a drop down box which is being populated by enteries from database. But the first value in the DropDownList is not working. Please help me out

@model IEnumerable<MvcApplication.Models.Bookinfo>
@{
   ViewBag.Title = "NewRequest";
 }

<h2>NewRequest</h2>
@using (@Html.BeginForm("Reserve","Book" ))
{
   <table>
      <tr>
         <td>
            @Html.DropDownList("Bookname",Model.Select(m => new SelectListItem() 
                              {Text = m.Bookname, Value = m.Bookname }).ToList())
         </td>

      </tr>
      <tr> 
         <td> 
            <input type = submit value="Reserve">
         </td>
      </tr>
   </table>
}

Actualy it is giving null value. Can i do like this put some random text as the first element of the list like "select a book " and start the list from second element

Now I am facing another problem. it is not able to reserve any newly created book. and i can't seem to figure out the problem

 public ActionResult Reserve(string bookname,Issued issue)
    {
        try 
            {
        using (var db = new Database1Entities1())
        {
            var bookID= (from some in db.Bookinfoes where some.Bookname==bookname select some.Id).First();
          //  var issueddate = (from issuedate in db.Issueds where issuedate.BookId == bookID select issuedate.date).First();
                if (db.Issueds.Where(i=>i.BookId == bookID).FirstOrDefault().BookId != 0 )
                {
                    return RedirectToAction("AlreadyReserved");
                }

            var date = DateTime.Now.Date;
            issue.BookId = bookID;
            issue.date = date;
            db.Issueds.Add(issue);
            db.SaveChanges();
        }    
        }
            catch
            {
               return RedirectToAction("Index");
            }
        return View();
    }

It is always catching an exception and returning to the index page

Upvotes: 0

Views: 182

Answers (2)

MartinStettner
MartinStettner

Reputation: 29164

You can set the Selected Propertys of the first SelectListItem to true. Since you're already using a LINQ-Expression to build up the list, you could use something like (the second parameter in the lambda expression passed to Select receives the index of the element...):

@Html.DropDownList("Bookname", Model.Select((m,i) => new SelectListItem
                   {Text = m.Bookname, Value=m.Bookname, Selected = i==0}).ToList())

Edit: If you want to have some arbitrary text as your first item (like "Select a book"), you can achieve that by adding an additional SelectListItem in front of your books. This can be done using some "LINQ magic" like:

@Html.DropDownList("Bookname", 
    Enumerable.Repeat(new SelectListItem {Text = "Select a book", Value=null, Selected=True}, 1)
    .Concat(Model.Select((m,i) => new SelectListItem
      {Text = m.Bookname, Value=m.Bookname}))
    .ToList())

Note: Enumerable.Repeat is used to create a sequence containing exactly one element. Also in this case, you can set the Selected property of the first element to True and leave it False on the rest of the list.

Upvotes: 0

Neel
Neel

Reputation: 11721

You should give BookId as the value as shown below :-

 @Html.DropDownList("Bookname",Model.Select(m => new SelectListItem() 
                              {Text = m.Bookname, Value = m.BookId}).ToList())

Upvotes: 1

Related Questions