Asp.net 修改索引方法的列显示数据
我有一个模型班Asp.net 修改索引方法的列显示数据,asp.net,asp.net-mvc,asp.net-mvc-4,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有一个模型班 public class CustomerModel { public int CustomerId { get; set; } public string CustomerName { get; set; } public int CustomerPoints { get; set; } } 在控制器方法中 public ActionResult Index()
public class CustomerModel
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public int CustomerPoints { get; set; }
}
在控制器方法中
public ActionResult Index()
{
return View(db.Customers.ToList());
}
在CustomerPoints
列中,我得到的是整数。我想显示此字段的用户友好消息。
像
如果客户点的值小于10,则
客户积分值>10&<20银币
客户积分值>20和<30黄金
客户点数值>30&<40菱形
我该怎么做?请帮忙,我是MVC平台的新手。谢谢。保持模型类如下
public class CustomerModel
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public int CustomerPoints { get; set; }
public List<CustomerModel> CustomerList { get; set; }
}
公共类CustomerModel
{
public int CustomerId{get;set;}
公共字符串CustomerName{get;set;}
公共int客户点{get;set;}
公共列表客户列表{get;set;}
}
保持动作方法如下:
public ActionResult Index()
{
CustomerModel model=new CustomerModel();
model.CustomerList= (from n in db.Customers
select new CustomerModel
{
CustomerPoints=n.CustomerPoints < 10 ?
"bronze":n.CustomerPoints >10 &&
n.CustomerPoints <20 ? "silver"
}).ToList();
return View(model);
}
public ActionResult Index()
{
CustomerModel模型=新CustomerModel();
model.CustomerList=(从db.Customers中的n开始)
选择新CustomerModel
{
CustomerPoints=n。CustomerPoints<10?
“青铜”:n.客户点数>10&&
n、 CustomerPoints项目。CustomerPoints)
}谢谢您的回复。我可以使用ViewModel而不是保留
公共列表CustomerList{get;set;}来做同样的事情吗
在模型类中?您在客户点中使用了terneray操作符
,但我有两个以上的条件,如果这里有其他条件如何使用?您的代码块选择新的客户模型…
有错误,请检查一次。谢谢!!
public ActionResult Index()
{
CustomerModel model=new CustomerModel();
model.CustomerList= (from n in db.Customers
select new CustomerModel
{
CustomerPoints=n.CustomerPoints < 10 ?
"bronze":n.CustomerPoints >10 &&
n.CustomerPoints <20 ? "silver"
}).ToList();
return View(model);
}
@model CustomerModel
@{
}
<h2></h2>
<table class="table">
<tr>
<th>Customer Points </th>
</tr>
@foreach (var item in Model.CustomerList ) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CustomerPoints)
</td>
</tr>