【发布时间】:2017-05-09 09:32:51
【问题描述】:
我正在开发一个应用程序 asp.net MVC,它有一个文本框和两个下拉列表。用户可以输入一个数值并从列表中选择从-到进行处理。我的问题是尝试验证下拉列表,因为我尝试使用所需的验证但没有工作。我也尝试在发送空值时检查下拉菜单,但错误验证不起作用!
错误类型:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'fromCurrency.Name'.
控制器
[HttpPost]
public ActionResult Index(Currencies cur)
{
if (ModelState.IsValid)
{
if (String.IsNullOrWhiteSpace(cur.fromCurrency.Name) || String.IsNullOrWhiteSpace(cur.toCurrency.Name))
{
ModelState.AddModelError(string.Empty, "you can not leave the empty dropdown please select any of these");
}
else
{
var fromCurrencyList = CurrenciesClient.GetFromCurrencyListAsync().Result;
ViewBag.FromCurrencies = new SelectList(fromCurrencyList, "CurrencyCode", "Name");
var ToCurrencyList = CurrenciesClient.GetToCurrencyListAsync().Result;
ViewBag.ToCurrencies = new SelectList(ToCurrencyList, "CurrencyCode", "Name");
var fromcurrname = cur.fromCurrency.Name;
string tocurrname = cur.toCurrency.Name;
//rate is taking by passing both dropdown currency code
decimal rate = CurrenciesClient.GetConversionRate("Currencies/GetConversionRate?fromcurrname=" + fromcurrname + "&tocurrname=" + tocurrname).Result;
ViewBag.TheResult = cur.CurrencyToConvert * rate;
}
}
return View();
}
索引视图
@model ViewModel.Currencies
@{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div id="ConversionSection">
<form class="form-horizontal" method="post" id= "CurrencyConversion" action="/Currency/Index">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="col-sm-4 control-label">
@Html.LabelFor(m => m.CurrencyToConvert, "Enter Currency")
</label>
<div class="col-sm-4">
@Html.EditorFor(m => m.CurrencyToConvert, new { @class = " form-control" })
@*@Html.ValidationMessageFor(m => m.CurrencyToConvert)*@
@Html.ValidationMessageFor(m => m.CurrencyToConvert, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">
@Html.LabelFor(model => model.fromCurrency.Name, "From Currency")
</label>
<div class="col-sm-4">
@Html.DropDownListFor(m => m.fromCurrency.Name, ViewBag.FromCurrencies as SelectList, "--select--", new { @class = " form-control" })
@Html.ValidationMessageFor(model => model.fromCurrency.Name)
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">
@Html.LabelFor(model => model.toCurrency.Name, "To Currency")
</label>
<div class="col-sm-4">
@Html.DropDownListFor(m => m.toCurrency.Name, ViewBag.ToCurrencies as SelectList, "--select--", new { @class = "form-control" })
@*@Html.ValidationMessageFor(model => model.toCurrency.Name)*@
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">
@Html.LabelFor(l => l.ConvertedCurrency, "Value")
</label>
<div class="col-sm-4">
@Html.Editor("TheResult", new { @class = " form-control" })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
Currencies.cs
public class Currencies
{
[Required]
[DataType(DataType.Currency)]
[DisplayFormat(ConvertEmptyStringToNull = false)]
public decimal CurrencyToConvert { get; set; }
public Currency fromCurrency { get; set; }
public Currency toCurrency { get; set; }
public double ConvertedCurrency { get; set; }
}
【问题讨论】:
标签: c# model-view-controller asp.net-mvc-5