MVC3中的下拉列表的验证消息 [英] validation message for dropdownlist in MVC3

查看:52
本文介绍了MVC3中的下拉列表的验证消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用select和option标签形成dropdownlist,如何将验证消息保留在mvc3中.

请找到dropDownlist的HTML代码

How to keep the validation message in mvc3 if dropdownlist is formed using select and option tags.

Please find the HTML code for the dropDownlist

<select name="SwList" style="width: 150px;">
    <option>--select-- </option>
    @foreach (var Sw in Model.SwDetails)
    {
        if (SwDetails.SwId == @Sw.SwId)
        {
        <option selected="selected" value=@Sw.SwId >
            @Sw.SwName

        </option>

        }
        else
        {
        <option value=@Sw.SwId >
            @Sw.SwName
        </option>

        }
       @*  @Html.ValidationMessageFor(Sw => Sw.SwId)*@
    }
</select>

推荐答案

最好有一个模型来收集所选项目的ID以及可能的选项列表.

例如遵循这些思路

Better to have a model that collects the ID of the selected item, as well as your list of possible options.

e.g. Soemthing along these lines

// View model
public class MyViewModel
{
    public int SelectedValue {get; set;}

    public IEnumerable<swdetails> {get; set;}
}

// Controller
public ActionResult ShowMyView
{
    var model = new MyViewModel();
    model.SwDetails = // However you get your list of items    
}

// View
@model MyViewModel

@Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.RatingScaleOptions, "SwId", "SwName"), "[Select One]")
@Html.ValidationMessageFor(m => m.SelectedValue)</swdetails>



因此,您可以使用"SelectValue"来检索由用户选择项目设置的值.验证与此字段相对.



So, you use the ''SelectValue'' to retrieve the value that is set by your user picking an item. Validation is against this field.


这篇关于MVC3中的下拉列表的验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆