这可以通过两种方式来实现,一种是在 ViewModel 类中设置值,或者在 ViewBag、ViewData 或 TempData 中设置值。
方式1)首选方式强类型:将css类名设置为viewmodel类属性:
Class Student
{
public ID BIGINT {get; set;}
... //other properties
}
Class StudentViewModel : Student
{
public CssClass string {get; set;}
}
//控制器动作
public ActionResult Index(){
StudentViewModel objModel;
//initialize model
objModel.CssClass = "myCssClass"; //set css class name to viewmodel
return View(objModel);
}
//查看使用如下代码:
@model namespace.StudentViewModel;
@Html.ActionLink("Manage List", "Index", new { @class = Model.CssClass })
方式2)将css类名设置为viewbag/viewdata/tempdate。但这不是首选。
//控制器动作
public ActionResult Index(){
ViewBag.CssClass = "myCssClass"; //set css class name to ViewBag
//or
ViewData["CssClass"] = "myCssClass"; //set css class name to ViewData
//or
TempData["CssClass"] = "myCssClass"; //set css class name to TempData
return View();
}
//查看使用如下代码:
@Html.ActionLink("Manage List", "Index", new { @class = @ViewBag.CssClass })
//Or
@Html.ActionLink("Manage List", "Index", new { @class = @Convert.toString(ViewData["CssClass"]) })
//Or
@Html.ActionLink("Manage List", "Index", new { @class = @Convert.toString(TempData["CssClass"]) })
请告诉我,这对你有用吗?