views:

41

answers:

2

I have this

<%=Model.StartDate%>
<%=Html.Hidden("StartDate", Model.StartDate)%>

it outputs:

 2010-05-11 11:00:00 +01:00      
 <input type="hidden" value="2010-03-17 11:00:00 +01:00" name="StartDate" id="StartDate"> 

What the...

It's a paging mechanism so the hidden value was valid on the first page and I've been able to move forward to the next page. But since the values won't update properly it ends there.

What do I need to do?

Using firefox.

Update - more code

using (Html.BeginForm("Program", "Activities", null, FormMethod.Get, new { @name = "ProgramForm", id = "ProgramForm" }))
{ 

.

        viewModel.StartDate = pagingService.StartDate;
        return View(viewModel);

Update - complete action

    [Authorize]
    public ActionResult Program(string[] submit)
    {
        var viewModel = new ActivityProgramViewModel { UserID = LoggedInUser.UserID };
        viewModel.Fresh = true;

        TryUpdateModel(viewModel);

        var pagingService = new OccurencePagingService(LoggedInUser.AllActivities.Where(a => a.StartTime != null));

        if (!viewModel.Fresh)
        {
            pagingService.StartDate = ((DateTimeOffset)viewModel.StartDate);
            pagingService.EndDate = ((DateTimeOffset)viewModel.EndDate);
        }

        if (submit != null)
            if (submit.Contains("MoveBack"))
                pagingService.MoveBack();
            else if (submit.Contains("MoveForward"))
                pagingService.MoveForward();

        ViewData.Model = viewModel;

        viewModel.Occurrences = pagingService.GetOccurences();
        viewModel.Fresh = false;

        viewModel.HasLess = pagingService.HasLess;
        viewModel.HasMore = pagingService.HasMore;

        viewModel.StartDate = pagingService.StartDate;
        viewModel.EndDate = pagingService.EndDate;

        return View();
    }
+4  A: 

First one uses Model object, second one uses existing ModelState. Look at ModelState values before generating view. It propably holds value for this field. Html helpers priveded by MVC use ModelState to generate form fields. It helps in recreating values after post back.

To get rid of this kind of problems, use POST-REDIRECT-GET pattern or just pass query parameters through GET.

LukLed
I'm obviously missing something here. ModelState["StartDate"] holds the value being put to the hidden-element, the value I don't want. So what would you have me do? I'm surprised cause I've been doing this sort of thing for a while now and I can't see what I'm doing differently on this view.
Martin
@Martin: Why do you post this hidden value? If you have to post this value, use redirection and pass only necessary values.
LukLed
You're right, I have no idea. Changed the method to get, but it didn't make a difference. PRG pattern seems a bit over the top. I'm just paging. Only the viewmodel is being updated.
Martin
How did you change it? Could you show us some of your code and what you send in get request?
LukLed
A: 

I think the <%=Html.Hidden("StartDate", Model.StartDate)%> is out of place here.

Html Helpers try to keep data in the UI like they where entered by examining the post/route data. Please dont ask me how someone would ever enter data in a hidden field.

You want something different: You want to set the data to Model.StartDate and dont care what is in the post/route.

I would use <input value="<%=Model.StartDate%>" name="StartDate" /> .

Malcolm Frexner
That is what I'm doing currently. I considered it a cheat/hack. But there you go.
Martin