I am reading a lot of posts since Last week but there is nothing about my problem.
I have this simple Controller:
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);
}
}
And this simple View :
@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")
)
)
Now the problem is the DropDownList. I can render it very easy like this:
@Html.DropDownList("Products", null , new { style = "color:red;" })
But I want to add the @item.Name to show the default String, and THAT is the problem, it is not working the @ (at sign) inside of the @HTML.DropDownList, it isn't yellow, it is like turn off because the @ in the beginning of the line like I posted in the beginning.
Any Idea how to solve this without creating a new SelectList(blah blah), just converting the @item.Name to String?
FIND THE SOLUTION AFTER 6 days trying and trying!!!!!!!!!!!!
@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")
)
)
I had to create a new string with the @item.Name and that's all!!! ufff I saw so many post where people where creating a ViewBag, and later creating a new list in the DropDOwnList that it didn't make sense...well here is the answer!!!