我想在一个下拉列表中显示两个值作为长度×宽度,如mvc4 db中的12×6 [英] i want to show two values in one dropdownlist as length × width like 12 × 6 in mvc4 db first
本文介绍了我想在一个下拉列表中显示两个值作为长度×宽度,如mvc4 db中的12×6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的观点:
this is my view:
<div class="editor-label">
@Html.DisplayName("Length") |
</div>
<div class="editor-field">
@Html.DropDownList("Size_ID",ViewBag.Size_ID as SelectList)
@Html.ValidationMessageFor(model => model.Size_ID)
</div>
<div class="editor-label">
@Html.DisplayName("Width")
</div>
<div class="editor-field">
@Html.DropDownList("Size_ID",ViewBag.Size_ID2 as SelectList)
</div>
这是我的控制器:
this is my controller:
// GET: /Carpet/Create
public ActionResult Create()
{
ViewBag.Size_ID = new SelectList(db.Sizes, "Size_ID", "Length");
ViewBag.Size_ID2 = new SelectList(db.Sizes, "Size_ID", "width");
return View();
}
//
// POST: /Carpet/Create
[HttpPost]
public ActionResult Create(Carpet carpet)
{
if (ModelState.IsValid)
{
db.Carpets.Add(carpet);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Size_ID = new SelectList(db.Sizes, "Size_ID", "Length", carpet.Size_ID);
ViewBag.Size_ID2 = new SelectList(db.Sizes, "Size_ID", "width", carpet.Size_ID);
return View(carpet);
}
和width by length方法是:
and for width by length method is:
public ActionResult Create()
{
//var sizeList=from s in db.Sizes
ViewBag.Size_ID = new SelectList(db.Sizes, "Size_ID", string.Format("{0}X{1}", "Length", "width"));
//ViewBag.abc = ViewBag.Size_ID;
return View();
}
推荐答案
在db.Carpets模型中..你还需要一个名为MesurementSize的属性..这将是concate length +'x'+ width ..并且你可以在下拉列表中显示...而不是只有hight或width ...你可以在从数据库中检索数据后进行连接...
In db.Carpets model.. you need to have one more property with name MesurementSize.. which will concate length + 'x' + width.. and that you can show in dropdown... instead of only hight or width... you can do concatenation after retrieving data from database...
这篇关于我想在一个下拉列表中显示两个值作为长度×宽度,如mvc4 db中的12×6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文