我想选择一个冒号的值具有where条件(它来自idlist的值)。然后这个值将添加到我试过的fiyatlist.Code下面。
怎么做?
public ActionResult Create(string UrunId, List<String> idlist)
{
List<String> fiyatList = new List<string>();
if (idlist != null)
{
ViewBag.abc = idlist;
int i;
for(i=0;i<idlist.Count();i++)
{
var sorgu = from a in db.Urunlers where a.UrunId == Int32.Parse(idlist[i]) select new { a.UrunFiyat};
fiyatList.Add(sorgu.ToString());
}
ViewBag.fiyat = fiyatList;
}
return View();
}
但是东西返回的是查询而不是值
UrunList查看
@foreach (BillApplication.Models.Urunler con in Model)
{
<tr>
<td>
<input id="urunsec" type="checkbox" name="cbList" value="@con.UrunId.ToString()" onchange="ClickMe(this)" />
</td>
<td>@con.UrunId</td>
<td>@con.UrunAdi</td>
<td><div id="=fiyatdiv"> @con.UrunFiyat </div></td>
<td>@con.AltkategoriId</td>
<td colspan="4"></td>
</tr>
}
<tr>
<td>
<input id="submit" type="submit" value="Gönder" name="Command" />
</td>
</tr>
创建视图
<td>
<textarea id="txt_urunler" rows="2" cols="20" style="border-style:inset; width:150px; border-width:0.2em; border-color:gainsboro">
@if (@ViewBag.abc != null)
{
foreach (var i in ViewBag.abc)
{
@i
}
}
</textarea>
</td>
<td>
<textarea id="txt_fiyat" rows="2" cols="20" style="border-style:inset; width:150px; border-width:0.2em; border-color:gainsboro">
@if (@ViewBag.fiyat != null)
{
foreach (var i in ViewBag.fiyat)
{
@i
}
}
</textarea>
</td>
制作行动
public ActionResult Create(string UrunId, List<String> idlist, List<String> fiyatList)
{
if (idlist != null)
{
ViewBag.abc = idlist;
}
if (fiyatList != null)
{
ViewBag.fiyat = fiyatList;
}
return View();
}
[HttpPost]
public ActionResult Create(Faturalar mReg, string Command, string txt_cari)
{
if (Command == "hesapla") { }
else if (Command == "Ürün Seç") { return RedirectToAction("UrunList"); }
return View();
}
答案 0 :(得分:0)
试试这个
foreach(var item in idlist)
{
int reqInt;
if(!int.TryParse(item.Trim(), out reqInt)
{
throw new Exception("Invalid Value");
}
var sorgu = (from a in db.Urunlers
where a.UrunId == reqInt
select a.UrunFiyat).FirstOrDefault();
fiyatList.Add(sorgu.ToString());
}
OR
var finalList = db.Urunlers.Where(x => idlist.Select(int.Parse).ToList().Contains(x.UrunId)).Select(v => v.UrunFiyat).ToList();
答案 1 :(得分:0)
您不应该创建额外的对象,在此示例中(sorgu)
这里你创建的sorgu没有用于进行任何进一步的计算。
for(i = 0; i < idlist.Count(); i++)
{
fiyatList.Add(db.Urunlers.Where(a => a.UrunId == int.Parse(idlist[i])
.FirstOrDefault()).ToString());
}
为清洁代码创建一个lamda。它是你的选择。
您也应该使用Model而不是ViewBag。