views:

954

answers:

3

I'm having a problem with something that I'm sure is very simple. I have been using Asp.Net MVC and I decided to start using Asp.Net MVC 2. Something has changed and now I need a little help. The strongly typed helpers are now written like this -

<%= Html.TextBoxFor(model => model.State) %>

I need to add a default value to a textbox. In the prior version of Asp.Net MVC it was easy to assign a default value. I thought doing the following would work in MVC 2-

<%= Html.TextBoxFor(model => model.CountyId, new{ value = 840 })%>

This, however, does not work for me in Asp.Net MVC 2. The value is still blank for the textbox. I want to make sure that this isn't some random error that I am having. Has anyone else encountered the same problem? I have searched and searched to find more information on the default property for the html helpers in MVC 2, but I can't find anything. Does anyone out there know how to correctly assign a default value to a textbox in Asp.Net MVC 2?

+4  A: 

If it's a constant value you could assign the default value to the property in your model instead (you can set it in your constructor or in a backing field if you use 'old-style' properties). Something like:

public class Model
{
   public int CountryId { get; set; }

   public Model()
   {
      this.CountryId = 840;
   }
}

Or if it varies per request, then set it in a View Model that you pass to your view from your controller.

Dan Diplo
Is this the only way? I have realized that properties like MaxLength still work correctly like this <%= Html.TextBoxFor(model => model.CountryID, new { maxlength = "40" })%>. Has value just been removed or is this the only way to add a default value in MVC 2 now?
Sara
I haven't checked, but this seems like the best way to do it, as it removes logic from the View. View's should just display data, not set it. However, there is nothing stopping you using the old-style Html.TextBox helpers or, indeed, hand-coding the HTML.
Dan Diplo
As I think about it, I know it can be done the way you outlined, but I think this may be a bug in MVC 2. For instance, you can still set all of the other properties of the text box using the new { whateverProperty = value }. It is ONLY the value property that appears to not be working. I think that must be a bug. What if you wanted to add a default property to say "Enter text here" or something like that? That isn't business logic. There has to be some way that I'm missing. I'm going to go over to MS Connect to see if this is a bug or intended new behavior. I will post their response here.
Sara
I don't think it's a bug, since when you use `model => model.CountyId` *and* also say `new{ value = 840 }` then you are creating a contradiction. What does the helper render, the value from the model or the value you are trying to force in via the attributes? I think if you want true default values they are best added to the model and if you want to add things like "enter text here" they are best done by javascript/css/jQuery or the like. Just my personal view!
Dan Diplo
This is correct -- you should always specify the DATA in the DATA MODEL. The empty constructor is what MVC uses when it creates your models, and it sets your properties after creation.
Andrew Dunkman
A: 

OK. I have found the answer to my problem - sort of. The new Html.TextBoxFor in MVC 2 doesn't allow the setting of the value property using the object htmlattributes even though it retains the ability to set all of the other properties using this syntax-

<%= Html.TextBoxFor(model => model.CountryName, new { maxlength = "40" })%>

So to get around this issue, if you need to set a default value on a textbox field, you need to use the old syntax -

<%= Html.TextBox("CountryName", "Enter your country name")%>

That will add a default value property appropriately in the html. The new TextBoxFor specification can't be used in this instance, but the value will still be returned back to the controller in the same way as the lambda expression. ALL of the other properties of the textbox can be set using textboxfor (maxlength, etc.) using the new { whateverProperty = value } syntax - just not the value property.

Sara
+4  A: 

Actually, in case anyone else has this problem, using Value instead of value works. I think the issue is that value with a common v is a c# keyword.

dannie.f
this seems like a bug to me, to allow a cased Html attribute through and override a value that is defined on the view model property. Interesting to know it works though!
Russ Cam