开发者

Html.EditorForModel and format of display

开发者 https://www.devze.com 2022-12-20 18:29 出处:网络
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:

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("Vehi开发者_高级运维cleDetails")]
    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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消