简体   繁体   中英

Passing value to select list in ASP.net

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 class="form-group">
   <select class="form-control" 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?

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.

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 class="form-group">
    <select class="form-control" asp-items="ViewBag.TranslatorList">
        <option>Select Translator</option>
    </select>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM