本文介绍了在组合2的SelectList领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的SelectList表单上写入ID,并显示出名字。

  ViewBag.Person =新的SelectList(db.Person,ID,名字);

如何连接名和姓成的SelectList?是这样的:

  ViewBag.Person =新的SelectList(db.Person,ID,名字和放大器;姓氏);


解决方案

尝试是这样的:

  ViewBag.Person =
新的SelectList((从s在db.Person选择新{
    ID = s.ID,
    全名= s.FirstName ++ s.LastName})
    ID,
    全名,
    空值);

或新的属性添加到您的Person模型

 公共字符串全名
{
    得到
    {
        返回的String.Format({0} {1},名字,姓氏);
    }
}

Currently I have SelectList that writes ID, and shows FirstName on the form.

ViewBag.Person = new SelectList(db.Person, "ID", "FirstName");

How to concatenate FirstName and LastName into SelectList? Something like:

ViewBag.Person = new SelectList(db.Person, "ID", "FirstName & LastName");
解决方案

Try something like this:

ViewBag.Person = 
new SelectList((from s in db.Person select new { 
    ID=s.ID,
    FullName = s.FirstName+ " " + s.LastName}), 
    "ID", 
    "FullName", 
    null);

Or Add a new property to your Person model

public string Fullname 
{
    get 
    {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}

这篇关于在组合2的SelectList领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:26