简体   繁体   中英

ASP.NET Mvc InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' Error

I am making a page. I am trying to make a DropDownList on this page. There should be two items in this DropDownList . I am trying to determine these two items through the Controller.

But I am getting an error like this:

在此处输入图像描述

My codes:

Index.cshtml :

@model IncomeAndExpensiveWeb.Models.IncomeExpensiveTable
@{
    ViewData["Title"] = "Money";
}

<div class="p-3 mb-2 bg-dark text-white">Add</div>
<form class="form-control" action="IncomeExpensiveTables/AddRecord" method="post">
    <div class="row">
        <div class="col">
            <div class="mb-3">
                <label for="Name" class="form-label">Name:</label>
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
            </div>
        </div>
        <div class="col">
            <div class="mb-3">
                <label for="Surname" class="form-label">Surname:</label>
                @Html.TextBoxFor(m => m.Surname, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label>
                <select class="form-control">
                    @foreach (var item in (IEnumerable<IncomeExpensiveTable>)ViewBag.MoneyStatusList)
                    {
                        <option value="@item.Id">@item.MoneyStatus</option>
                    }
                </select>
            </label>
        </div>  
        <div class="form-group">
            <label>Image:</label>
            <input type="File" name="UploadedImage"/>
        </div>
        <br/>
        <div>
            <button class="btn btn-success">Add</button>
        </div>
    </div>
</form>

Controller:

[HttpGet]
public ActionResult Index()
{
    List<string> MoneyStatusList = new List<string>();
    MoneyStatusList.Add("Income");
    MoneyStatusList.Add("Expensive");
    ViewBag.MoneyStatusList = MoneyStatusList;
    return View("Index");
}

Why could this be? How can I solve it? Thanks for help.

select class="form-control">
                    @foreach (var item in (IEnumerable<IncomeExpensiveTable>)ViewBag.MoneyStatusList)
                    {
                        <option value="@item.Id">@item.MoneyStatus</option>
                    }

"IncomeExpensiveTable" is string on code behind. Thats why you can't cast.

You have to cast it your ViewBag model type which is List() and only use just item there is no any item.Id or item.MoneyStatus.

Like this:

 <select class="form-control">
                @foreach (var item in (List<string>)ViewBag.MoneyStatusList)
                {
                    <option value="@item">@item</option>
                }
  </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM