gpt4 book ai didi

asp.net-mvc - 如何创建具有所需验证的 ASP.Net MVC DropDownList

转载 作者:行者123 更新时间:2023-12-02 18:04:03 25 4
gpt4 key购买 nike

我使用 mvc 5。我正在使用 ORM 从数据库加载数据,并从 Controller 填充下拉列表,如下所示。

ViewBag.Country_id = new SelectList(_db.Countries, "Country_id", "Description");

因为我首先想要一个空字段,所以我在 HTML 中执行此操作。

<div class="form-group">
@Html.LabelFor(model => model.Countries, "Country", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Country_id", null, htmlAttributes: new { @class = "form-control" }, optionLabel: "Choose a Country")
@Html.ValidationMessageFor(model => model.Country_id, "", new { @class = "text-danger" })
</div>
</div>

空选项的值为“0”。

我想验证用户选择的国家/地区,因此我添加了此验证

[Required,Range(1, int.MaxValue, ErrorMessage = "Error: Must Choose a Country")]
public int Country_id { get; set; }

问题是永远不会给我带来错误。始终为“0”并且未进行验证。

我缺少什么?

最佳答案

使用DropDownList的方法有很多种。我个人喜欢使用强类型ViewModel而不是ViewBag

屏幕截图

在未选择国家/地区的情况下单击提交按钮时会显示验证消息。

enter image description here

实体

public class Country
{
public int Country_id { get; set; }
public string Description { get; set; }
}

型号

public class CountryViewModel
{
[Display(Name = "Country")]
[Required(ErrorMessage = "{0} is required.")]
public int SelectedCountryId { get; set; }

public IList<SelectListItem> AvailableCountries { get; set; }

public CountryViewModel()
{
AvailableCountries = new List<SelectListItem>();
}
}

Controller

public class HomeController : Controller
{
public ActionResult Create()
{
var countries = GetCountries();
var model = new CountryViewModel {AvailableCountries = countries};
return View(model);
}

[HttpPost]
public async Task<ActionResult> Create(CountryViewModel countryViewModel)
{
if (ModelState.IsValid)
{
int countryId = countryViewModel.SelectedCountryId;
// Do something
}
// If we got this far, something failed. So, redisplay form
countryViewModel.AvailableCountries = GetCountries();
return View(countryViewModel);
}

public IList<SelectListItem> GetCountries()
{
// This comes from database.
var _dbCountries = new List<Country>
{
new Country {Country_id = 1, Description = "USA"},
new Country {Country_id = 2, Description = "UK"},
new Country {Country_id = 3, Description = "Canada"},
};
var countries = _dbCountries
.Select(x => new SelectListItem {Text = x.Description, Value = x.Country_id.ToString()})
.ToList();
countries.Insert(0, new SelectListItem {Text = "Choose a Country", Value = ""});
return countries;
}
}

查看

@model DemoMvc.Models.CountryViewModel
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>

<h2>Create</h2>

@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.SelectedCountryId,
new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedCountryId,
Model.AvailableCountries, new {@class = "form-control"})
@Html.ValidationMessageFor(model => model.SelectedCountryId,
"", new {@class = "text-danger"})
</div>
</div>

<input type="submit" value="Submit"/>
}

</body>
</html>

关于asp.net-mvc - 如何创建具有所需验证的 ASP.Net MVC DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com