Ich arbeite an einer Anwendung asp.net MVC, die ein Textfeld und zwei Dropdown-Listen hat. Benutzer können einen numerischen Wert eingeben und aus der Liste von - bis auswählen. Mein Problem versucht, die Dropdown-Liste zu validieren, während ich versuchte, die erforderliche Validierung zu verwenden, aber nicht funktionierte. Ich habe auch versucht, die Dropdowns zu überprüfen, wenn Nullwerte gesendet werden, aber die Fehlerüberprüfung funktioniert nicht!Eine Null-Validierung aus der Dropdown-Liste schreiben MVC
Der Fehlertyp:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'fromCurrency.Name'.
Contorller
[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();
}
Übersicht
@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; }
}
Alles funktioniert gut! Vielen Dank für die Beantwortung der Frage. – Dodi