Home > Blockchain >  Passing value to select list in ASP.net
Passing value to select list in ASP.net

Time:11-22

I am trying to pass a concatenated columns from a LINQ statement to a select list using view bag.

Controller:

var translators = bidsinfo1.Select(x => new { Name = x.TranslatorFirstName   " "   x.TranslatorLastName} ).ToList();
ViewBag.TranslatorList = translators;

View:

<div >
   <select  asp-items="new SelectList(ViewBag.TranslatorList)">
                    <option>Select Translator</option>
   </select>
</div>

But in the select list when i run the project it's showing the values like this { Name = harvey specter }, please any suggestion?

CodePudding user response:

u should convert List(of oBject) to a List(of String) and pass it to viewBag so change your linqQuery to

var translators = bidsinfo1.Select(x =>  x.TranslatorFirstName   " "   x.TranslatorLastName ).ToList();

GoodLuck.

CodePudding user response:

i tried this and it's work now..

var translators = bidsinfo1.Select(x => new { Id = x.Id, Name = x.TranslatorFirstName   " "   x.TranslatorLastName });
ViewBag.TranslatorList = new SelectList(translators, "Id", "Name");
<div >
    <select  asp-items="ViewBag.TranslatorList">
        <option>Select Translator</option>
    </select>
</div>
  • Related