1

I am using a ViewModel to Create a new item in my DB the ViewModel has only the properties that I want the user to be able to set, and when it is posted back I make a new 'real' object and save it away to the DB.

I am doing this as detailed below

[HttpGet]
public ActionResult Create(int id = 0)
{
    var opt = unitOfWork.OptionRepository.GetByID(id);

    CreateAvailabilityViewModel model = new CreateAvailabilityViewModel();
    model.OptionDescription =  opt.Description;
    model.CentreCode = opt.CentreCode;
    model.OptionID = id;

    return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateAvailabilityViewModel cavm)
{
    if (ModelState.IsValid)
    {
        OptionAvailability newOA = new OptionAvailability();
        DateTime now = DateTime.Now;

        newOA.ChangedDate = newOA.CreatedDate = now;
        newOA.ChangedBy = newOA.CreatedBy = User.Identity.Name;

        newOA.DateFrom = cavm.DateFrom;
        newOA.DateTo = cavm.DateTo;
        newOA.MinNumbers = cavm.MinNumbers;
        newOA.MaxNumbers = cavm.MaxNumbers;
        newOA.OptionID = cavm.OptionID;

        unitOfWork.OptionAvailabilityRepository.Insert(newOA);
        unitOfWork.Save();

        return RedirectToAction("Detail", "Option", new { id = newOA.OptionID });
    }

    return View(cavm);
}

and this is the ViewModel

public class CreateAvailabilityViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int OptionAvailabilityID { get; set; }
    [HiddenInput(DisplayValue = false)]
    public int OptionID { get; set; }
    [Required]
    public DateTime DateFrom { get; set; }
    [Required]
    public DateTime DateTo { get; set; }
    [Required]
    public int MinNumbers { get; set; }
    [Required]
    public int MaxNumbers { get; set; }
    public string CentreCode { get; set; }
    public string OptionDescription { get; set; }
}

the problem I am facing is that when the form is rendered the form fields for the dates and ints are defaulting to 01/01/0001 and 0 instead of being blank. I am using the Html.EditorFor helpers to render the inputs I assume it is because in the HttpGet Create method, when I instantiate the ViewModel it uses the type defaults and then passes them through in the object to the form, but this is not wha tI want to be happening.. do I need to set these properties to DateTime? and/or int? ?

enter image description here

I am pretty sure this is good practice to use but am a bit stumped as to why can anyone explain what I am doing wrong please thanks muchly

nat
  • 2,185
  • 5
  • 32
  • 64

1 Answers1

0

You can instantiate the dates with whatever values you want.

You could use backing fields in your viewmodel (instead of auto properties) and initialize them:

public class MyViewModel 
{
    private DateTime _firstDate = new DateTime(12/12/2012);
    private DateTime _secondDate = DateTime.Now();

    public DateTime FirstDate { get { return _firstDate; } set { _firstDate = value; } }
    ...
}

Or you could initialize them in the viewmodel's constructor:

public class MyViewModel 
{
    public MyViewModel(DateTime firstDate)
    {
        FirstDate = firstDate;
        SecondDate = DateTime.Now();
    }

    public DateTime FirstDate { get; set; }
    ....
}

Or you could initialize them in your controller; you probably get the idea.

Also consider decorating these members with metadata:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime FirstDate { get; set; }
  • hi, thanks for the reply, but I don't want to have a default at all, I want them to be blank.. is this not achievable ? – nat Apr 28 '14 at 08:15
  • Any proper date/datetime control is of course browser-specific, and as far as I can tell, the spec is a bit hazy on a reasonable default value for date/datetime (or whether null/blank is ever semantically valid); at this point that approach is probably unpredictable. If you're outputting a vanilla text input(s), you could use javascript to clear the value when the page loads. Separate GET/POST viewmodels seems like overkill. –  Apr 28 '14 at 14:10