自上周以来,我正在阅读很多帖子,但我的问题没有任何内容。
我有这个简单的控制器:
public class HomeController : Controller
{
private AdventureWorks2014Entities db = new AdventureWorks2014Entities();
public ActionResult Index()
{
var model = db.Products.ToList();
ViewBag.Products = new SelectList(db.Products, "ProductID", "Name");
return View(model);
}
}
这个简单的观点:
@model IEnumerable<ADWork.Client.Models.Product>
@{
var grid = new WebGrid(Model);
}
@grid.GetHtml(
columns: grid.Columns(
grid.Column("ProductID"),
grid.Column("Name", null, format:@<span>@Html.DropDownList("Products", @item.Name, new { style = "color:red;" }) </span>),
grid.Column("ListPrice", "Price")
)
)
现在问题是DropDownList。我可以像这样轻松地渲染它:
@Html.DropDownList("Products", null , new { style = "color:red;" })
但是我想添加@ item.Name来显示默认的String,那就是问题,它不能在@HTML.DropDownList里面的@(at符号)里工作,它不是黄色的,它就像关闭,因为@在行的开头就像我在开头发布的那样。
任何想法如何在不创建新的SelectList(blah blah)的情况下解决这个问题,只需将@ item.Name转换为String?
答案 0 :(得分:0)
在尝试和尝试6天后找到解决方案!!!!!!!!!!!!
@grid.GetHtml(
columns: grid.Columns(
grid.Column("ProductID"),
grid.Column("Name", null, format:
@<span>
grid.Column("Name", null, format:
@<span>
@{
string name = (@item.Name).ToString();
@Html.DropDownList("Products", null, name , new { style = "color:red;" });
}
</span>),
</span>),
grid.Column("ListPrice", "Price")
)
)
我必须使用@ item.Name创建一个新字符串,并且全部! ufff我看到很多帖子都是创建ViewBag的人,后来在DropDOwnList中创建了一个新的列表,它没有意义......这就是答案!