views:

613

answers:

1

I'm using MVC 2, and the Html.EditorForModel() to allow me to display an editor for the model.

I am using a model which looks something like this:

public class LoanACar
{
    [DisplayNameFromResource("VehicleDetails")]
    public string VehicleDetails { get; set; }
    [DisplayNameFromResource("VehicleId")]
    [Required]
    public long VehicleId { get; set; }
    [DisplayNameFromResource("LoanCentreId")]
    [Required]
    public long LoanCentreId { get; set; }
    [DisplayNameFromResource("StartDate")]
    [Required]
    public DateTime StartDate { get; set; }
    [DisplayNameFromResource("EndDate")]
    [Required]
    public DateTime EndDate { get; set; }
}

Which is working great.

I'd like to take it further now and have my VehicleDetails to be a label, not a textbox, also the VehicleId to be a hidden.

+2  A: 

There is a special attribute System.Web.Mvc.HiddenInputAttribute for your purposes.

public class LoanACar
{
    [DisplayNameFromResource("VehicleDetails")]
    [HiddenInput(DisplayValue = true)]
    public string VehicleDetails { get; set; }

    [DisplayNameFromResource("VehicleId")]
    [Required]
    [HiddenInput(DisplayValue = false)]
    public long VehicleId { get; set; }

    [DisplayNameFromResource("LoanCentreId")]
    [Required]
    public long LoanCentreId { get; set; }

    [DisplayNameFromResource("StartDate")]
    [Required]
    public DateTime StartDate { get; set; }

    [DisplayNameFromResource("EndDate")]
    [Required]
    public DateTime EndDate { get; set; }
}

Consider difference in DisplayValue parameter value of the HiddenInput attribute.

Anton