私は、テキストボックスと2つのドロップダウンリストを持つアプリケーションasp.net MVCの開発に取り組んでいます。ユーザーは数値を入力し、〜から〜までのリストから選択できます。私の問題は、必要な検証を使用しようとしたが、動作しなかったので、ドロップダウンリストを検証しようとしています。 null値を送信するときにドロップダウンをチェックしようとしましたが、エラーの検証は機能しません。ドロップダウンリストからnull検証をポストするMVC
エラーの種類:
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();
}
インデックスビュー
@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; }
}
すべてうまくいっています!質問にお答えいただきありがとうございます。 – Dodi