如何在View中绑定性别列 [英] How do I bind gender column in View
问题描述
嗨朋友们,
我是MVC的新手,我有一个包含Gender列的DB员工表。性别列有数据1,2,其中1表示男性,2表示女性。现在当我绑定它的实体框架时,它只绑定1和2但实际上我需要男性和女性格式。
提前致谢
在你的视图中,添加一个剃刀代码如下所示。
您可能拥有顶部=
@ model IEnumarable < 员工 >
和itarate通过这个列表,比如,
< 表格 >
< ; thead >
< tr > < th > 名称< / th > < / tr >
< tr > < th > 性别< / th > < ; / tr >
< thead >
< ; tbody >
@ {
foreach(模型中的var项)//这里的项是一个Employee Model对象
{
< tr >
var GenderText =Male;
if(item.Gender == 2){GenderText =Female}
< td >
< 输入 类型 = text 名称 = 名称 value = item.Name / >
< / td >
< td >
< 输入 类型 = text name = 性别 value = @ GenderText / >
< / td >
< / tr >
}
}
< / tbody >
< / table >
还有其他方法可以实现这一点,例如拥有ViewModel或ModelBinder。我觉得上面的一个是最好的,我在我的项目中多次使用它。
希望这有帮助。
你好朋友们,
我在这里找到了解决方案..
http://stackoverflow.com/questions/8549455/if-else-statement-in-razor-is-not-functioning [< a href =http://stackoverflow.com/questions/8549455/if-else-statement-in-razor-is-not-functioningtarget =_ blanktitle =New Window> ^ ]
Hi Friends,
I'm new to MVC, I've a DB employee table which contains a Gender column. The gender columns has data 1,2 which 1 means Male and 2 means Female. Now when Im binding it thriugh entity framework, it is binding 1 and 2 only but actually i need Male and Female format.
Thanks in advance
Inside your view, add a razor code Like below.
You may have on top =
@model IEnumarable<employee>
and itarate through this list like,
<table> <thead> <tr><th>Name</th></tr> <tr><th>Gender</th></tr> <thead> <tbody> @{ foreach(var item in Model) // Here item is one Employee Model object { <tr> var GenderText = "Male"; if(item.Gender == 2){GenderText = "Female"} <td> <input type=text name="Name" value="item.Name"/> </td> <td> <input type="text" name="Gender" value="@GenderText" /> </td> </tr> } } </tbody> </table>
There are other way to acheive this like having a ViewModel or ModelBinder. I feel the above one is best and I have used it many times in my project.
Hope this helps.
Hi Friends,
I found the solution here..
http://stackoverflow.com/questions/8549455/if-else-statement-in-razor-is-not-functioning[^]
这篇关于如何在View中绑定性别列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!