0

I am trying to create a form with MVC4 and so far it is going well, but I have run into a problem where is doesn't seem possible to give my inputs a default value by setting the associated 'value' attribute.

The code that I am using:

@Html.TextBoxFor(x => x.Name, new { value="xxx" })

renders the following:

<input id="EditDetail_SKU" name="EditDetail.SKU" type="text" value="" />

That is kind of annoying because the value attribute is now empty. It seems that I can add all sorts of attributes after the fact, and they are respected, ala:

@Html.TextBoxFor(x => x.Name, new { value="xxx", thing="whatever",foo="bar" })

Yields:

<input foo="bar" id="name" name="Name" thing="whatever" type="text" value="" />

Those attributes are even made up, but they are still respected, so why is 'value' being ignored in this case, and is there something I can do prevent this?

tereško
  • 58,060
  • 25
  • 98
  • 150
A.R.
  • 15,405
  • 19
  • 77
  • 123

3 Answers3

1

Please refer to this thread: How to set a default value with Html.TextBoxFor?

The quick and dirty method would be:

<%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %>

Notice the @ and capital V

Community
  • 1
  • 1
Marco
  • 22,856
  • 9
  • 75
  • 124
  • Right, of course, @ and captial V? I see in the other thread nobody knows why this works either, ha ha. – A.R. Oct 23 '13 at 14:42
  • That's why I wrote it below, too. It's easy to ignore for people having the same problem as you and just look fopr an answer. – Marco Oct 23 '13 at 14:43
0

The default value will be the value of your Name property inside model.

This is how strongly typed models work in ASP.NET MVC.

In your respective action assign the Name variable to default value.

public ActionResult Index()
{ 
    var model = new YourModel();
    model.Name = "Default Value";

    return View(model);
}

And in your view,

@Html.TextBoxFor(x => x.Name) 
emre nevayeshirazi
  • 18,983
  • 12
  • 64
  • 81
  • Care to explain downvote ? This is the correct way to do this. – emre nevayeshirazi Oct 23 '13 at 14:35
  • Your answer ignores the content and spirit of the question. Look at the accepted answer, which is actually the 'correct' way to do it. – A.R. Oct 23 '13 at 14:41
  • 1
    Maybe it's a bit harsh to downvote a working solution, just because it is not a universal working solution. – Marco Oct 23 '13 at 14:46
  • @A.R. you didn't mention where you want to set the default value. And most of the time, you set the default values in actions or somewhere else where the default value is calculated according to some business logic. This is why I said this is the correct way. If you want quick, tricky way to solve your problem fine ! But it does not make my answer off topic. – emre nevayeshirazi Oct 23 '13 at 14:57
  • From the very first sentence in the question: *"but I have run into a problem where is doesn't seem possible to give my inputs a default value by setting the associated 'value' attribute."* In particular: **setting the associated 'value' attribute."** I'd say that is pretty clear as to how and where I want to set the value. – A.R. Oct 23 '13 at 15:11
0

Use @Value instead of value. value is a keyword.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • 'value' is not a keyword in this context, nor will the lowercase version of 'value' work. Please see the accepted answer. – A.R. Oct 23 '13 at 14:36