//显示字段
public static string GetItemName(string strType, int itemId)
{
SqlConnection con = new SqlConnection(sqlcon);
string comtext = "select ItemName from TypeConfing where itemId = " + itemId + "and type = '"+ strType +"'";
SqlCommand commond = new SqlCommand(comtext, con);
con.Open();
string ItemName = (string)commond.ExecuteScalar();
con.Close();
return ItemName;
}
在数据库建立一个根据字段翻译的类,进行调用,在主表中写入ID根据ID去翻译的配置文件中查找对应我翻译字段。
然后进行调用。
namespace JiaBanModel
{
public class TypeConfing
{
//数据库下拉类型
public string Type { get; set; }
public int ItemID { get; set; }
public string ItemName { get; set; }
}
}
然后在显示列表或者需要翻译中进行调用。
SHItemID = Convert.ToInt32(sdr["SHItemID"]),
SHItemName = GetItemName("SHZT", Convert.ToInt32(sdr["SHItemID"])),
然后在前台更改数据显示类型修改字段
<td>
@Html.DisplayFor(modelItem => item.SHItemName)
</td>
这就是本人初学时候翻译字段的方法。
这样整个程序需要翻译字段就可以去配置文件中添加数据然后根据字段进行翻译本人还有下拉
@Html.DropDownList("SHItemID", ViewBag.SHZT as IEnumerable<SelectListItem>)
上面是在前台页面显示建立下拉菜单然后调用翻译进行对int类型的数字翻译成String字符串类型
List<SelectListItem> JBType = JiaBanInfoDAL.SelectList("JBLX");
ViewBag.JBType = JBType;
//控制台中
控制台中下拉写法如上