简体   繁体   中英

How can I make Html.ListBoxFor required

I know I can make DropDownListFor required in HTML page by adding required felid as below:

@Html.DropDownListFor(m => m.Site_idSite, Model.GetSiteList(Model.idCompany),  new { @class = "w_300p", required = "required" })

My question is how can I make ListBoxFor required?

@Html.ListBoxFor(m => m.SelectedSites, Model.GetSiteList(Model.idCompany), new { @class = "w_250p", Size = "4" })

Any help will be appreciated. Stuck with this problem quite a longg.

Can you try this for server side validation

public class ProgramViewModel
    {
        [Required(ErrorMessage = "Please select at least one company")]
        public IEnumerable<string> YourCompanyIdList{ get; set; }
    }

and also if you want to enable client side validation you can use this reference: https://www.codeproject.com/Articles/718004/ASP-NET-MVC-Client-Side-Validation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM