0

最后编辑:问题是 ViewBag.PropName == Model.PropName; 修复:ViewBag.PropName ===> ViewBag.PropName2

这是我第一次发帖;我是新来的,我认为我搞砸了布局......很抱歉!我遇到了一些隐藏下拉列表的客户端验证问题。我正在使用 ASP.NET MVC3,并且在此页面上有很多隐藏的 ddl。但是,验证不适用于某些 ddl。以前有人遇到过这个问题吗?

这是2个属性;1 工作,1 失败

//VALIDATION FAILS
[Required]
[Range(0, 4)]
public int TiresBack { get; set; }

//VALIDATION WORKS
[Required]
[Range(0, 4)]
public int TechnicalState { get; set; }

这是一块剃须刀:

<tr>//THIS DOESN'T WORK
    <td style="width: 38%;">  
        @txtFor("tiresBack")
    </td>
//This is JQuery Star-Rating.
    <td align="center" id="starsTiresBack">
    </td>
    <td>                            
        @Html.DropDownListFor(model => model.TiresBack, ViewBag.TiresBack as IEnumerable<SelectListItem>, new { style = "display:none;" })
        <input class="blueButton" type="button" id="btnBrandRearTires" value="@txtFor("brandTiresBack")" />
        <span>@Html.ValidationMessageFor(model => model.TiresBack)</span> <span>@Html.ValidationMessageFor(model => model.BrandRearTires)</span>
    </td>
</tr>
 //THIS WORKS
<tr>
    <td style="width: 38%;">
        @txtFor("technicalState")                            
    </td>
    <td id="starsTechnicalState" align="center">
    </td>
    <td>
        @Html.DropDownListFor(model => model.TechnicalState, ViewBag.TechState as IEnumerable<SelectListItem>, new { style = "display:none;" })
        <input class="blueButton" type="button" id="btnTechnicalStateDescription" value="@txtFor("technicalStateDescriptionButton")" style="display:none;"/>
        <span>@Html.ValidationMessageFor(model => model.TechnicalState)</span> <span>@Html.ValidationMessageFor(model => model.TechnicalStateDescription)</span>
    </td>
</tr>

EDIT1:使用此方法在控制器中初始化:

    public static List<SelectListItem> CreateDefaultStateList(int? selected = -1)
    {
        List<SelectListItem> sl = new List<SelectListItem>();
        for (int i = -1; i < 5; i++)
        {
            SelectListItem sli = new SelectListItem();
            if (i == 0 || i == -1)
                sli.Text = "";
            else
                sli.Text = i.ToString();
            sli.Value = i.ToString();
            if (i == selected)
                sli.Selected = true;
            sl.Add(sli);
        }
        return sl;
    }

EDIT2:创建控制器。defaultstatelist 是从上面发布的方法返回的内容。

List<SelectListItem> defaultStateList = CreateDefaultStateList();

[Authorize]
public ActionResult Create()
{
    FillViewBag();
    ViewBag.PropX = defaultStateList;
    ViewBag.TiresFront = defaultStateList;
    ViewBag.TechState = defaultStateList;
    ViewBag.PropY= defaultStateList;
    ViewBag.PropZ= defaultStateList;
    ViewBag.PropA= defaultStateList;
    ViewBag.PropB= defaultStateList;
    ViewBag.PropC= defaultStateList;
    ViewBag.PropD= defaultStateList;  

    return View(); 
}

输入这个我注意到方法中的默认值。可能是这样... ==> 试过了,不是吗。现在将代码更新为“-1”作为默认值。

4

1 回答 1

0

问题在于ViewBag.PropName == Model.PropName; FIX: 重命名ViewBag.PropNameViewBag.PropName2.

于 2012-05-16T11:09:46.100 回答