如何从Controller发送CustomerName和CustomerSurname以编辑或创建cshtml

时间:2015-11-22 11:22:34

标签: asp.net-mvc drop-down-menu entity-framework-6

我想从Controller发送CustomerName和CustomerSurname来编辑或创建cshtml但我收到的错误是:

  

System.Data.Entity.DynamicProxies.CUSTOMERS_14601EA232CC1C78E8EEE3B8325EDF47D2BEC1CCBF414A98771B931D28B27715'   不包含名称为' CustomerName的属性   CustomerSurname'

控制器

// GET: PAYMENT/Create
public ActionResult Create()
{
    ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName" + " " + "CustomerSurname");
    return View();
}

CSHTML

@Html.ValidationSummary(true)
<div class="form-group">
    @Html.LabelFor(model => model.PaymentCustomer, "PaymentCustomer", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("PaymentCustomer", String.Empty)
        @Html.ValidationMessageFor(model => model.PaymentCustomer)
    </div>
</div>

有谁知道如何同时使用两个列名? &#34;客户名称&#34; +&#34; &#34; +&#34; CustomerSurname&#34;

尝试使用控制器/编辑进行编辑时

 // GET: PAYMENT/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            PAYMENT pAYMENT = db.PAYMENT.Find(id);
            if (pAYMENT == null)
            {
                return HttpNotFound();
            }
            ViewBag.PaymentCustomer = new SelectList(db.CUSTOMERS, "CUSTID", "CustomerName", pAYMENT.PaymentCustomer);
            ViewBag.PaymentLocation = new SelectList(db.LOCATION, "LOCID", "LocationName", pAYMENT.PaymentLocation);
            return View(pAYMENT);
        }

修改视图

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

1 个答案:

答案 0 :(得分:2)

您可以生成IEnumerable<SelectListItem>并设置Text属性

ViewBag.PaymentCustomer = db.CUSTOMERS.ToList().Select(c => new SelectListItem
{
  Value = c.CUSTID.ToString(),
  Text = string.Format("{0} {1}", c.CustomerName, c.CustomerSurname)
});
return View();