Asp.net mvc 我的应用程序有两个下拉列表,最小年龄和最大年龄。不希望允许最大年龄值小于最小年龄值

Asp.net mvc 我的应用程序有两个下拉列表,最小年龄和最大年龄。不希望允许最大年龄值小于最小年龄值,asp.net-mvc,dropdownbox,Asp.net Mvc,Dropdownbox,在我的ASP.NET MVC应用程序中,我有两个下拉列表,最小年龄和最大年龄。我不希望允许选择的最大年龄下拉值小于最小年龄下拉值 我的看法是: 下拉列表1分钟年龄: @Html.DropDownListFor(m => m.MinAge, new SelectList(ViewBag.agelist, "AgeValue", "AgeValue") 下拉列表2最大年龄: @Html.DropDownListFor(m => m.MaxAge, new SelectList(View

在我的ASP.NET MVC应用程序中,我有两个下拉列表,最小年龄和最大年龄。我不希望允许选择的最大年龄下拉值小于最小年龄下拉值

我的看法是:

下拉列表1分钟年龄:

@Html.DropDownListFor(m => m.MinAge, new SelectList(ViewBag.agelist, "AgeValue", "AgeValue")
下拉列表2最大年龄:

@Html.DropDownListFor(m => m.MaxAge, new SelectList(ViewBag.agelist, "AgeValue", "AgeValue"))
控制器:

 public ActionResult PartnerPreference()  
 {
     ViewBag.agelist = mdal.AgeList.ToList < AgeList > ();
     return View();
 }
[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        if (Convert.ToInt32(collection["MinAge"]) > Convert.ToInt32(collection["MaxAge"]))
        {
            ModelState.AddModelError(string.Empty, "Min Age should be greater than Max Age");
            return View(cl);
        }

        // TODO: Add insert logic here

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}
例如:

If end-user select the min age vale of 25 from the Min age dropdown list 
then it should not allow max age dropdown values less then min age values.(select only above 25 values {25,26,27,28,29,30}).
如何编写代码将条件和验证消息放入我的web应用程序中

我希望你能理解我的问题


谢谢

您可以在控制器中使用类似的功能:

 public ActionResult PartnerPreference()  
 {
     ViewBag.agelist = mdal.AgeList.ToList < AgeList > ();
     return View();
 }
[HttpPost]
public ActionResult Create(FormCollection collection)
{
    try
    {
        if (Convert.ToInt32(collection["MinAge"]) > Convert.ToInt32(collection["MaxAge"]))
        {
            ModelState.AddModelError(string.Empty, "Min Age should be greater than Max Age");
            return View(cl);
        }

        // TODO: Add insert logic here

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

您可以从FormCollection中检索MinAge和MaxAge,然后比较它们,如果MinAge大于MaxAge,则向模型添加一个错误,这将向最终用户显示一个错误

使用条件验证属性,例如
[GreaterThan()]
属性