How to send CustomerName and CustomerSurname from Controller to edit or create cshtml

╄→尐↘猪︶ㄣ 提交于 2019-12-23 04:54:16

问题


I want to send CustomerName and CustomerSurname from Controller to edit or create cshtml but I am getting error as:

System.Data.Entity.DynamicProxies.CUSTOMERS_14601EA232CC1C78E8EEE3B8325EDF47D2BEC1CCBF414A98771B931D28B27715' does not contain a property with the name 'CustomerName CustomerSurname'.

The Controler

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

The 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>

Does anyone knows how to use both column name as one? "CustomerName" + " " + "CustomerSurname"

And when try to edit with controller/edit

 // 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);
        }

Edit View:

    <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:


You can generate IEnumerable<SelectListItem> and set the Text property

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


来源:https://stackoverflow.com/questions/33854197/how-to-send-customername-and-customersurname-from-controller-to-edit-or-create-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!