Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 2 - Need To Add A Default Property To A Strongly Typed Html.Textbox Helper In Asp.Net MVC 2

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?

like image 566
Sara Avatar asked Apr 22 '10 21:04

Sara


People also ask

What is strongly typed view in ASP NET MVC?

Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure. By specifying the type of data, you get access to IntelliSense for the model class.

What are strongly typed helpers in MVC?

The Strongly-Typed HTML helper takes a lambda as a parameter that tells the helper, which element of the model to be utilized in the typed view. The Strongly typed views are used for rendering specific sorts of model objects, rather than using the overall View-Data structure.

What is the difference between Html TextBox and Html TextBoxFor using ASP NET MVC Razor engine?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.


1 Answers

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.

like image 57
dannie.f Avatar answered Sep 28 '22 06:09

dannie.f