Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted

Inside your view, add a razor code Like below.

You may have on top =
HTML
@model IEnumarable<employee>



and itarate through this list like,

HTML
<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.
 
Share this answer
 
v5
Comments
Rockstar_ 22-May-15 9:21am    
Hi Pradeep, this is not working. var GenderText = "Male";
if(item.Gender == 2){GenderText = "Female"} code is giving error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900