1

The model below used in two ways:

public class SimpleModel
{
    public DateTime? Date { get; set; }

    // Some other properties

    public SimpleModel()
    {
         Date = DateTime.Now;
    }
}

When model is used in the form, generated URL have empty parameter Date (/Controller/Action?Date=&SomeOtherParams=123) and Date property in model is null (after submitting form with empty date).

...

@Html.TextBoxFor(model => model.Date)

...

Also this model used as third parameter in UrlHelper.Action():

@Url.Action("Action", "Controller", Model) // Model is SimpleModel

In this case if Date is null, generated URL does not contains parameter Date (/Controller/Action?SomeOtherParams=123). And if I following this URL, property Date is DateTime.Now, not null as expected.

How to force passing empty properties to URL?

UPD. Action code

public ActionResult MyAction( SimpleModel model = null )
{
     if ( model.Date == null )
     {
        // show all
     }
     else
     {
        // show by date
     }
}

Actually, instead of TextBoxFor used DropDownListFor.

@Html.DropDownListFor( model => model.Date, Model.Dates)

User can choose Date from DropDown or live it empty if he want to see all entities.

If user submiting form with empty Date, he following URL /Controller/Action?Date= and getting all entities (Date property was initialized with default value in constructor and then overriten with null).

If user following by generated url via @Url.Action from other page (not submitting form), he getting only todays entities, because URL not contains Date parameter (/Controller/Action). In this case Date property initializing in constructor and this is all.

Problem is model in MyAction never equals null and I cant recognize when user selects empty Date and when he just visit page without parameters.

Bob Sponge
  • 4,708
  • 1
  • 23
  • 25
  • I found very dirty solution: `@Url.Action( "Action", "Controller", Model )&Date=` will generate `/Controller/Action?SomeOtherParams=123&Date=` – Bob Sponge Jan 16 '13 at 11:25

2 Answers2

1

If you want the Date to be null, then remove the line that assigns DateTime.Now in the empty constructor.

public SimpleModel()
{
   //This line is assigning the value to Date whenever a new SimpleModel() is created. Comment it out.
   //  Date = DateTime.Now;  

}
robasta
  • 4,621
  • 5
  • 35
  • 53
1

And if I following this URL, property Date is DateTime.Now, not null as expected.

The problem is that the MVC engine is trying to construct a Model object based on the constructor you defined, which sets the Date property to DateTime.Now. It's advised that you do not define a constructor with empty parameters as it will be used by the MVC engine to create the model on POST. As it stands, this is expected behavior.

If you need the functionality, then define another constructor:

public SimpleModel() // Left for MVC
{
}

public SimpleModel(DateTime date) // Use as: new SimpleModel(DateTime.Now) in your action 
{
     Date = date;
}
rae1
  • 6,066
  • 4
  • 27
  • 48
  • I describe it incorrectly, yes, this is expected behavior. And I using this expected behavior to set `Date` with default value in constructor. Problem is I cant recognize when user selects empty Date and when he just visit page without parameters (is this case `Date` must have default value, not null). – Bob Sponge Jan 16 '13 at 15:39
  • So, you want `Date` to be `null` so you can check whether is was selected or not, right? In this case, this will never happen since you are always setting `Date` to `DateTime.Now` in your constructor. Now, is the problem is passing the `Model` through the `Url.Action` method, you gonna run into problems since parsing the model. You'll better off passing a date by itself to the `Action`. – rae1 Jan 16 '13 at 16:01