views:

36

answers:

1

I have a table [Users] with the following columns:
INT SmallDateTime Bit Bit
[UserId], [BirthDate], [Gender], [Active]

Gender and Active are Bit that hold either 0 or 1.

I am displaying this data in a table on my View.

  1. For the Gender I want to display 'Male' or 'Female', how and where do I manipulate the 1's and 0's? Is it done in the repository where I fetch the data or in the View?

  2. For the Active column I want to show a checkBox that will AutoPostBack on selection change and update the Active filed in the Database. How is this done without Ajax or jQuery?

A: 

For Question #1:

Use a DisplayTemplate. In View/Shared create a folder named DisplayTemplates. Then Add a partial View to this folder named Gender.ascx. Make sure it is not strongly typed.

the markup in this file will look like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Boolean>" %>
<span><%= Model ? "Male" : "Female" %></span>

Then in your Model you decorate the Gender property with a UIHint thusly:

[UIHint("Gender")]
public boolean Gender { get; set; }

In your View you make it use this template by using DisplayFor

<%= Html.DisplayFor(model => model.Gender) %>

If you cannot use the UIHint you can explicity name your template in DisplayFor

<%= Html.DisplayFor(model => model.Gender, "Gender") %>
Cheddar