Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用.NETMVC5从要在DropDownList中显示的两个分离列中获取数据_C#_.net_Asp.net Mvc - Fatal编程技术网

C# 如何使用.NETMVC5从要在DropDownList中显示的两个分离列中获取数据

C# 如何使用.NETMVC5从要在DropDownList中显示的两个分离列中获取数据,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我是ASP.NETMVC5的新学员开发人员。插入时,我需要从SQL中的两个单独的表列中获取一个数据,但我只在DropDownList中获取一个值。我该怎么做?根据我的研究,没有适当的信息或来源。有人帮我吗 型号: public class PersonelModel { public int pid { get; set; } public string pAd { get; set; } public string pSoyad {

我是ASP.NETMVC5的新学员开发人员。插入时,我需要从SQL中的两个单独的表列中获取一个数据,但我只在DropDownList中获取一个值。我该怎么做?根据我的研究,没有适当的信息或来源。有人帮我吗

型号:

public class PersonelModel
    {  
        public int pid { get; set; }
        public string pAd { get; set; }
        public string pSoyad { get; set; }
        public string yonetici { get; set; } 
    }
我的添加方法:

public ActionResult PersonelAdd()
{

    /*it does not work
     ViewBag.yonetici = new SelectList(db.Personel, "pAd", "pAd" +" "+ "pSoyad");*/

    /*it work*/
    ViewBag.yonetici = new SelectList(db.Personel, "pAd", "pAd");

    return View();
}

[HttpPost]
public ActionResult PersonelAdd(Personel pModel)
{
    var personel = new Personel();
    personel.pAd = pModel.pAd;
    personel.pSoyad = pModel.pSoyad;
    personel.yonetici = pModel.yonetici;
    /*it works*/
    ViewBag.yonetici = new SelectList(db.Personel, "pAd", "pAd", pModel.yonetici
    );

    // it does not work 
    // ViewBag.yonetici = new SelectList(db.Personel, "pAd", "pAd"+" "+"pSoyad", pModel.yonetici);

    db.Personel.Add(personel);
    db.SaveChanges();
    return RedirectToAction("Index", "AdminUI");
}
我的表格:

 <div class="form-group">
            @Html.LabelFor(model => model.yonetici, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("yonetici", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.yonetici, "", new { @class = "text-danger" })
            </div>
        </div>

@LabelFor(model=>model.yonetci,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“yonetci”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.yonetci,“,new{@class=“text danger”})
采用必须存在于模型类中的字段名(
dataValueField
dataTextField

您可以添加新字段来描述要显示为文本的内容(请注意新建
Description
property):

现在,您可以将pAd+pSoyad显示为SelectList的文本值:

ViewBag.yonetici = new SelectList(db.Personel, "pAd", "Description")

当我尝试添加字符串描述值时,我在PersonelModel.cs中遇到了一个错误,如“只能将赋值、调用、递增、递减和新对象表达式解析为语句”。因此出现了一个错误。它成功地在DropDownList中显示了“pAd pSoyad”。但在向数据库插入数据时,它只向SQL表发送pAd。[HttpPost]部分是否应该用于发送pAd+pSoyad?是的,我认为您应该在将其发送到控制器之前修改视图以填充整个PersonelModel。DropDownList仅设置一个属性(valueproperty pAd)。
ViewBag.yonetici = new SelectList(db.Personel, "pAd", "Description")