3

I have a weird problem in my MVC app.

When the user selects a date from a drop down, it clears the StartDate and EndDate fields.

I have the following fragment of code:

    <label>Start date: @Model.StartDate</label>
    @Html.TextBoxFor(s => s.StartDate)

The weird thing is that you can wee where I'm outputting it in the label, the date comes out there. The textbox is unpopulated.

I've checked the produced markup and the textbox is not being populated.

    <label>Start date: 19/05/2013</label>
    <input id="StartDate" name="StartDate" type="text" value="" />        <br />

What am I missing here?

To add a little bit more information, when the page is initially populated the default start and end date are output. There is a bit of jQuery that empties those fields when a <select> is changed. If I comment that bit out then the fields retain their previous values as opposed to blank. Essentially, whatever is submitted to the server is output rather than the value in the model.

Sam
  • 4,219
  • 7
  • 52
  • 80
  • You have the solution in a similar stackoverflow question http://stackoverflow.com/questions/1961114/date-only-from-textboxfor – Alberto León May 29 '13 at 06:41
  • Not sure what is going on. Your code works fine on my side. the textbox is populated correctly. Maybe you need to add more code. – Jason Li May 29 '13 at 06:47
  • @AlbertoLeón the field in the model is a string, thanks anyway. – Sam May 29 '13 at 06:51
  • @JasonLi I would but I really don't know what's relevant given that the field is output blank and the label isn't. – Sam May 29 '13 at 06:57
  • the logical reason could be that you don't set with text the StartDate string field. Is empy. So isn't any wrong with TextBoxFor, is that you don't populated the field. Please give us the code that is supposed setting the field StartDate – Alberto León May 29 '13 at 09:55
  • @AlbertoLeón Then could you explain why the label field populated? – Jason Li May 29 '13 at 13:44
  • @JasonLi Because the model and the dinamyc "s" objects aren't the same object – Alberto León May 29 '13 at 19:39
  • That makes no sense. The fact that the label shows the correct value and the text field isn't doesn't make sense. – Sam May 30 '13 at 01:58
  • @AlbertoLeón How could be different object? Could you give an example? – Jason Li May 30 '13 at 04:58

3 Answers3

2

Essentially, whatever is submitted to the server is output rather than the value in the model.

This behaviour is actually by design. The idea being that generally the user would expect to see in the text box what they submitted to the server.

See here for a detailed explanation, and a work around.

StanK
  • 4,750
  • 2
  • 22
  • 46
0

Instead of doing this

   <label>Start date: @Model.StartDate</label>
    @Html.TextBoxFor(s => s.StartDate)

You should do this

   <label id="someId"></label>
    @Html.TextBoxFor(s => s.StartDate,new{@id="startdate"})

and using jquery on change event on your textbox you can set lablel

$("#startdate").change(function(){
var date="Start Date:"+$(this).val();
$("#someid").html(date);
});
Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87
0

Thinks that your model is a class named What like this:

  public class What
    {
        public string StartDate { get; set; }
    }

Then, think that your application is "MyApplication", you need to add to the view as if the view is stronglytyped:

@using MyApplication.Models;
@inherits System.Web.Mvc.WebViewPage<What>

Then all should we run as you expect

Alberto León
  • 2,879
  • 2
  • 25
  • 24