Ondrej Sotolar
Ondrej Sotolar

Reputation: 1442

Convert Dropdownlist item's value to int

I need the value of SelectListItem to be int.

So I pull it of the database, convert it to string in the process and store to listitem's value.

public class BookAdd
{
public BookAdd()
{
public Book Book { get; set; }
DataModelContainer db = new DataModelContainer();
IEnumerable<SelectListItem> items = db.PublisherSet
               .Select(i => new SelectListItem
               {
                 Value = SqlFunctions.StringConvert((double)i.Id), 
                 Text = i.Name
               });
    }
}

I then need to store the value again as int to Book.PublisherId when selected from dropdownlist. I know the code below is not complete, I figured I need somehow convert the selected item's value to int, how do I do it?

@model Bookshops.Models.BookAdd
...
@Html.DropDownListFor(model => model.Book.PublisherId, Model.items);

And finaly controler:

    public ActionResult Create2()
    {
        var bookAdd = new BookAdd();
        ViewBag.Publisher = bookAdd.items;
        return View(bookAdd);
    }

    //
    // POST: /Book/Create

    [HttpPost]
    public ActionResult Create2(BookAdd book)
    {
            if (ModelState.IsValid)
            {
                Book book2 = new Book();
                book2.Id = book.Book.Id;
                book2.AuthorId = book.Book.AuthorId;
                book2.Isbn = book.Book.Isbn;
                book2.Id = int.Parse(book.Book.PubId);
                book2.Title = book.Book.Title;
                book2.YearPublished = book.Book.YearPublished;

                db.BookSet.Add(book2);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
    }

I'm using MVC 3, EF 4.

Upvotes: 1

Views: 7400

Answers (3)

Raymond
Raymond

Reputation: 133

Instead of building a SelectListItem, I built something like this:

public struct BookItem
{
   int id;
   string name;
}

Then add the following item to the model and select data into it

IEnumerable<BookItem> bookList {get; set;}

Fianally

@Html.DropDownListFor(model => model.Book.PublicherId, new SelectList(Model.bookList, "id", "name"))

Upvotes: 3

Jason Meckley
Jason Meckley

Reputation: 7591

to start you can simplify the mapping code to

db
    .PublisherSet
    .Select(x=> new SelectListItem{Value = x.Id.ToString(), Text = x.Name})
    .ToArray();

In the view you can then do

@Html.DropDownListFor(model => model.Book.PublisherId, Model.items);

and it will properly render a select element with the options and the appropriate publisher selected.

You can then use the model binding conventions to pass this back to the controller action when the user clicks the submit button.

In the end, it's all text. that's the nature of the web. we have a lot of tricks, converters and binders to turn string dictionaries into rich view models, but ultimately it's just strings.

Upvotes: 0

HatSoft
HatSoft

Reputation: 11201

When the selected Item of the DropDownList will get posted back convert it to int from string like this Convert.ToInt32(DropDownSelectedItem) or with Int32.TryParse(value, out number)

Upvotes: 0

Related Questions