【问题标题】:Set required to select validation for dropdown list in asp.net razor在 asp.net razor 中设置为下拉列表选择验证所需的设置
【发布时间】:2014-03-06 09:07:22
【问题描述】:

如何在不使用模型的情况下在 asp.net razor 的下拉列表中应用选择所需的验证。 下面是我的下拉列表

<td>@Html.DropDownList("Units", new SelectList(@ViewBag.Settings.CdcPortalRentInfo 
         , "CdcPortalRentInfoId" , "Description")
         , new { style = "width:250px" })
</td>

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    在 MVC 中使用 required 属性尝试类似的操作

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

    【讨论】:

    • 适当地。编译器错误消息:CS1026:) 预期源错误:第 19 行: @Html.ValidationMessageFor(@ViewBag.Settings.CdcPortalRentInfo => @ViewBag.Settings.CdcPortalRentInfo.CdcPortalRentInfoId) 行22:
      单位类型:
    • 我应该怎么写@Html.ValidationMessageFor(@ViewBag.Settings.CdcPortalRentInfo => @ViewBag.Settings.CdcPortalRentInfo.CdcPortalRentInfoId)
    • 嗨 Jalpesh,我没有使用模型。我必须创建一个对象并放入 vewbag。我正在从 VewBag 检索视图中的对象并绑定到下拉列表。现在我需要提供验证。
    【解决方案2】:

    在您的模型中添加数据注释:

     [Required(ErrorMessage = "You must select a RentInfo")]
        public int CdcPortalRentInfoId{ get; set; }
        public int Description{ get; set; } //I think this is you property
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签