您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 初学者翻译数据库字段。下拉字段翻译,数据显示翻译

2018-06-08 15:34 555 查看
版权声明: https://blog.csdn.net/qq_41639782/article/details/80623910
//显示字段
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;
//控制台中
控制台中下拉写法如上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: