如何从Controller发送CustomerName和CustomerSurname来编辑或创建cshtml [英] How to send CustomerName and CustomerSurname from Controller to edit or create cshtml

查看:97
本文介绍了如何从Controller发送CustomerName和CustomerSurname来编辑或创建cshtml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从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>

有谁知道如何使用列名作为一个?
CustomerName++CustomerSurname



当尝试使用控制器/编辑编辑

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


解决方案

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

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


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>

解决方案

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();

这篇关于如何从Controller发送CustomerName和CustomerSurname来编辑或创建cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆