tags:

views:

21

answers:

1

I have a partial view that I use for editing and creating my models. When a user is editing the model, I would like a particular drop-down to be rendered as text as I don't want them to change it. A read-only drop-down would suffice, but I'd prefer text.

I'm thinking I can accomplish this via an extension method, but I'm not quite sure how to get started. Any help would be appreciated!

A: 

Here's what I tried (and it works) - though I'm not a fan of if/else's in my views :)

<% if (ViewContext.Controller.ValueProvider.GetValue("action").RawValue.Equals("Edit"))
       { %>
    <span><%= Event.Retailer.Name%></span>
    <%= Html.Hidden("Retailer", Event.Retailer.Id) %>
    <%}
       else
       { %>
    <%=Html.DropDownList("Retailer", Model.Retailers, "Select", new { @class = "field select large required" })%>
    <%= Html.ValidationMessage("Retailer", "*")%>
    <%} %>
Dan
Ah, I see what you are doing now. If you are using the same view for both creating and editing records, an `if` statement is unavoidable, as you need a way to *change the state of the view.* I generally just use two different views for `create` and `edit`.
Robert Harvey
Yea - I read somewhere that if you need an "if" statement - use a helper. I could do Html.Retailer(..) I suppose. I'd use a different view for `create` and `edit`, but the forms are so similar, I was able to group them
Dan