简体   繁体   English

ASP.NET Mvc InvalidCastException:无法转换类型为“System.Collections.Generic.List`1[System.String]”的 object 错误

[英]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.我正在尝试在此页面上创建一个DropDownList There should be two items in this DropDownList .这个DropDownList应该有两个项目。 I am trying to determine these two items through the Controller.我正在尝试通过 Controller 确定这两项。

But I am getting an error like this:但我收到这样的错误:

在此处输入图像描述

My codes:我的代码:

Index.cshtml : 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: 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. “IncomeExpensiveTable”是后面代码的字符串。 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.您必须将其转换为您的 ViewBag model 类型,即 List() 并且仅使用 item,没有任何 item.Id 或 item.MoneyStatus。

Like this:像这样:

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

无法投射“object {System.Collections.Generic.List”类型的 object<string[]> }”作为ICollection<object><div id="text_translate"><p> 为什么不能将 object 在运行时的类型为object {System.Collections.Generic.List&lt;string[]&gt;}为ICollection&lt;object&gt; ?</p><p> 我确定它很明显,但我无法弄清楚。</p><p> 它给出了例外 -</p><blockquote><p> 无法将类型为“System.Collections.Generic.List 1[System.String[]]' to type 'System.Collections.Generic.ICollection ]”。</p></blockquote></div></object></string[]> - Unable to cast an object of type “object {System.Collections.Generic.List<string[]>}” as ICollection<object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' asp.net json Web 服务发送数组错误无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - asp.net json web service sending array error Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' InvalidCastException:无法将“System.Guid”类型的 object 转换为“System.String”类型。 在 asp.net 核心 webapi - InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'. in asp.net core webapi 无法在 asp.net mvc 中将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable” - Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Collections.Generic.IEnumerable error in asp.net mvc 无法转换&#39;System.Collections.Generic.List`1类型的对象 - Unable to cast object of type 'System.Collections.Generic.List`1 asp.net错误:无法将&#39;System.DateTime&#39;类型的对象强制转换为&#39;System.String&#39; - asp.net error: Unable to cast object of type 'System.DateTime' to type 'System.String' 无法将类型为“System.Collections.Generic.List`1[System.Object]”的 object 转换为类型“System.Collections.Generic.List`1” - Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.List`1[customType] 无法将类型为“ System.String”的对象转换为类型为“ System.Collections.Generic.List`1” - Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1 无法投射“object {System.Collections.Generic.List”类型的 object<string[]> }”作为ICollection<object><div id="text_translate"><p> 为什么不能将 object 在运行时的类型为object {System.Collections.Generic.List&lt;string[]&gt;}为ICollection&lt;object&gt; ?</p><p> 我确定它很明显,但我无法弄清楚。</p><p> 它给出了例外 -</p><blockquote><p> 无法将类型为“System.Collections.Generic.List 1[System.String[]]' to type 'System.Collections.Generic.ICollection ]”。</p></blockquote></div></object></string[]> - Unable to cast an object of type “object {System.Collections.Generic.List<string[]>}” as ICollection<object> 无法将“System.Collections.Generic.List”类型的 object 转换为“System.Collections.Generic.ICollection”类型 - Unable to cast object of type 'System.Collections.Generic.List to type 'System.Collections.Generic.ICollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM