1

How can I apply the selection required validation on dropdownlist in asp.net razor without using model. below is my DropDownList

<td>@Html.DropDownList("Units", new SelectList(@ViewBag.Settings.CdcPortalRentInfo 
         , "CdcPortalRentInfoId" , "Description")
         , new { style = "width:250px" })
</td>
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62

2 Answers2

0

In your model just add data annotation :

 [Required(ErrorMessage = "You must select a RentInfo")]
    public int CdcPortalRentInfoId{ get; set; }
    public int Description{ get; set; } //I think this is you property
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62
0

try something like this with required attribute in MVC

public class CdcPortalRentInfo 
{
      [Required(ErrorMessage="Your Error Message")]
      public int CdcPortalRentInfoId{ get; set; }

       public string Description {get;set;}
}
Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94
  • See- http://stackoverflow.com/questions/8345763/required-validation-attribte-not-working-in-asp-net-mvc-3-while-others-work – Jalpesh Vadgama Mar 11 '14 at 12:19
  • See another one- http://stackoverflow.com/questions/19734608/required-attribute-not-working-in-asp-net-mvc – Jalpesh Vadgama Mar 11 '14 at 12:21
  • appropriately.Compiler Error Message: CS1026: ) expected Source Error:Line 19: @Html.ValidationMessageFor(@ViewBag.Settings.CdcPortalRentInfo => @ViewBag.Settings.CdcPortalRentInfo.CdcPortalRentInfoId) Line 22:
    Unit Type:Required Field
    – krishna menan Mar 11 '14 at 12:22
  • How should i write @Html.ValidationMessageFor(@ViewBag.Settings.CdcPortalRentInfo => @ViewBag.Settings.CdcPortalRentInfo.CdcPortalRentInfoId) – krishna menan Mar 11 '14 at 12:25
  • Hi Jalpesh,I am not using model. I have to created a object and putting into vewbag. I am retrieving the object in view from VewBag and binding to dropdownlist. Now I need to provide the validation. – krishna menan Mar 11 '14 at 12:33
  • why you need to have object? – Jalpesh Vadgama Mar 12 '14 at 06:33