【问题标题】:How can I overcome this error CS1579 in ASP.net?如何克服 ASP.net 中的 CS1579 错误?
【发布时间】:2020-05-01 22:36:08
【问题描述】:

报错如下:

System.Web.HttpCompileException: 'C:\Users\MEDADUWA\source\repos\CapeXPro\CapeXPro\Views\FAWizard_AddAsset.cshtml(43): 错误 CS1579:foreach 语句不能对类型变量进行操作 'FAInfo' 因为 'FAInfo' 不包含公共定义 'GetEnumerator''

我的 ViewModel 看起来像这样:

namespace DAL.Entities
{
    public class FAInfo
    {
        public int WizardId { get; set; }
        public string WizardType { get; set; }

        public Step2 Step2 { get; set; }

        public List<Step2> ListStep2 { get; set; }

        public int Status { get; set; }
    }

    public class Step2
    {
        public int assetId { get; set; }
        public string assetCode { get; set; }
        public int assetCategoryId { get; set; }
        public int assetTypeId { get; set; }
        public int branchId { get; set; }
        public int? ccenterId { get; set; }
        public string description { get; set; }
        public double cost { get; set; }
        public double? vat { get; set; }
        public double? vatRec { get; set; }
        public double? nbt { get; set; }
        public double bookValue { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime? ModifiedOn { get; set; }
        public string ModifiedBy { get; set; }
        public int Status { get; set; }

        public InvoiceEntities Invoice { get; set; }
        public List<Step2> ListAssets { get; set; }
        public List<tbl_assetcategory> ListCategories { get; set; }
        public List<tbl_branch> ListBranches { get; set; }
        public List<tbl_costcenter> ListDepartments { get; set; }
    }
}

这是我的控制器:

namespace CapeXPro.Controllers
    {
        [SessionExpire]
        public class FAWizardController : Controller
        {
            UnitOfWork _unitOfWork = new UnitOfWork();
            readonly CommonUtilities _commonService = new CommonUtilities();

            #region Main View
            FAInfo FAInfoComponent(FAInfo model = null)
            {
                try
                {
                    //var existjournaldepreciation = _unitOfWork.JournalDepreciationRepository.GetAll(x => x.Id == 1 && x.Status != (int)StatusEnum.Delete).FirstOrDefault();
                    if (model == null)
                    {
                        model = new FAInfo()
                        {
                            Status = (int)StatusEnum.Active,
                        };
                    }
                    return model;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            // GET: FAWizard
            public ActionResult FAWizard()
            {
                return View("FAWizard", FAInfoComponent());
            }
            #endregion

            #region Add Or Edit Step2
            [ValidateAntiForgeryToken]
            [HttpPost]
            public ActionResult Step2(FAInfo AssetData, string BtnPrevious, string BtnNext)
            {
                try
                {
                    var action = string.Empty;
                    var before = string.Empty;
                    var after = string.Empty;

                    AssetData = FAInfoComponent(AssetData);

                    if (BtnPrevious != null)
                    {
                        FAInfo InvObj = new FAInfo();

                        InvObj.Step1.InvoiceId = AssetData.Step2.Invoice.InvoiceId;
                        InvObj.Step1.InvoiceNo = AssetData.Step2.Invoice.InvoiceNo;
                        InvObj.Step1.PoNo = AssetData.Step2.Invoice.PoNo;

                        return PartialView("_AddInvoice", InvObj);
                    }
                    if (BtnNext != null)
                    {
                        if (ModelState.IsValid)
                        {
                            var existObj = _unitOfWork.AssetRepository.GetAll(x => x.assetId == AssetData.Step2.assetId && x.Status != (int)StatusEnum.Delete).FirstOrDefault();

                            if (existObj == null)
                            {
                                tbl_asset asset = new tbl_asset()
                                {
                                    assetId = AssetData.Step2.assetId,
                                    assetCode = AssetData.Step2.assetCode,
                                    assetCategoryId = AssetData.Step2.assetCategoryId,

                                    CreatedBy = System.Web.HttpContext.Current.Session[SessionsEnum.UserId.ToString()].ToString(),
                                    CreatedOn = DateTime.Now,
                                    Status = Convert.ToInt32(AssetData.Status)
                                };

                                _unitOfWork.AssetRepository.Insert(asset);
                                //_unitOfWork.Save();
                                //int id = department.CcId;

                                action = ActionType.Insert;
                                before = null;
                                after = asset.assetId + "," +
                                        asset.assetCode + "," +
                                        asset.assetCategoryId + "," +

                                        asset.Status + "," +
                                        asset.CreatedBy + "," +
                                        asset.CreatedOn + "," +
                                        asset.ModifiedBy + "," +
                                        asset.ModifiedOn;
                            }
                            else
                            {
                                FAInfo AssetObj = new FAInfo();

                                AssetObj.Step2.assetId = AssetData.Step2.assetId;
                                AssetObj.Step2.assetCode = AssetData.Step2.assetCode;
                                AssetObj.Step2.assetCategoryId = AssetData.Step2.assetCategoryId;

                                AssetObj.Step2.ModifiedBy = System.Web.HttpContext.Current.Session[SessionsEnum.UserId.ToString()].ToString();
                                AssetObj.Step2.ModifiedOn = DateTime.Now;
                                AssetObj.Step2.Status = Convert.ToInt32(AssetData.Status);

                                _unitOfWork.AssetRepository.Update(existObj);

                                action = ActionType.Update;
                                before = null;
                                after = existObj.assetId + "," +
                                        existObj.assetCode + "," +
                                        existObj.assetCategoryId + "," +

                                        existObj.Status + "," +
                                        existObj.CreatedBy + "," +
                                        existObj.CreatedOn + "," +
                                        existObj.ModifiedBy + "," +
                                        existObj.ModifiedOn;
                            }

                            if (_unitOfWork.Save() > 0)
                            {
                                //Update Log Table
                                AuditLog.SystemLog(Tables.AssetTable, action, before, after);

                                TempData["message"] = Messages.Success;
                                ModelState.Clear();
                                return View("AddDepreciation", new FADepreciationEntities());
                            }
                            else
                            {
                                TempData["message"] = Messages.Failed;
                                return PartialView("_AddAsset", AssetData);
                            }
                        }
                        return PartialView("_AddAsset", AssetData);
                    }
                    return View();
                }
                catch (Exception ex)
                {
                    TempData["message"] = Messages.Exception + ex.Message;
                    return PartialView("_AddAsset", AssetData);
                }
            }
            #endregion

        }
    }

这是我的局部视图

@model DAL.Entities.FAInfo
@using DAL.Enums

@using (Html.BeginForm("Step2", "FAWizard", FormMethod.Post, new { @class = "form-horizontal form-label-left", role = "form", enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(m => m.Step2.assetId)
    <!-- Editable table -->
    <div class="card">
        <div class="card-body">
            <div id="table" class="table-editable">
                <span class="table-add float-right mb-3 mr-2">
                    <a href="#!" class="text-success">
                        <i class="fas fa-plus fa-2x" aria-hidden="true"></i>
                    </a>
                </span>
                <table id=""
                       class="table table-striped table-hover table-sm table-bordered text-center">
                    <thead>
                        <tr>
                            <th class="text-center">Line No</th>
                            <th class="text-center">Asset Code</th>
                            <th class="text-center">Asset Class</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (DAL.Entities.FAInfo asset in Model)
                        {
                            <tr>
                                <td>1</td>
                                <td>@asset.Step2.assetCode</td>
                                <td>@asset.Step2.ListCategories</td>
                                <td>
                                    <span class="table-remove">
                                        <button type="button"
                                                class="btn btn-danger btn-rounded btn-sm my-0">
                                            Remove
                                        </button>
                                    </span>
                                </td>
                            </tr>
                        }

                    </tbody>
                </table>
            </div>
        </div>
    </div>
}

这是我的 MasterView:

@model DAL.Entities.FAInfo
@using DAL.Enums
@{
    ViewBag.Title = "FAWizard";
}

<div class="right_col" role="main">
    <div class="full-height">
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>Fixed Assets Capitalization <small>CapeXPro</small></h2>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <form class="form-horizontal form-label-left" novalidate>
                            <p>
                                For Fixed asset registration - CapeXPro | Union Bank of Colombo
                            </p>
                            <span class="section">Fixed Asset Info</span>
                            <!-- Smart Wizard -->
                            <p>Proceed with the given steps</p>
                            <div id="wizard" class="form_wizard wizard_horizontal">
                                <ul class="wizard_steps">
                                    <li>
                                        <a href="#step-1">
                                            <span class="step_no">1</span>
                                            <span class="step_descr">
                                                Step 1<br />
                                                <small>Invoice and Insurance Details</small>
                                            </span>
                                        </a>
                                    </li>
                                    <li>
                                        <a href="#step-2">
                                            <span class="step_no">2</span>
                                            <span class="step_descr">
                                                Step 2<br />
                                                <small>Asset Details</small>
                                            </span>
                                        </a>
                                    </li>   
                                </ul>
                                <div id="step-1">
                                    @{Html.RenderPartial("_AddInvoice", Model);}
                                </div>
                                <div id="step-2">
                                    @{Html.RenderPartial("_AddAsset", Model);}
                                </div>
                            </div>
                            <!-- End SmartWizard Content -->
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 您好,欢迎来到 Stackoverflow。请您将您共享的代码缩减为一个显示您所询问问题的最小示例。
  • 我在您的示例中的代码中看不到foreach,究竟是哪里抛出了错误?
  • 在您的View 中,我假设您有类似的内容:@model Models.FAInfo。将其更改为:IEnumerable&lt;Models.FAInfo&gt;
  • 编辑了代码,你们也可以看到部分视图...
  • 您想在视图中使用 single FAInfo(为什么还要使用 foreach?)或 multiple(为什么要提供单个一个在你的控制器中并且有一个单一的模型?)

标签: c# html asp.net-mvc asp.net-mvc-4 runtime-error


【解决方案1】:
System.Web.HttpCompileException: 'C:\Users\MEDADUWA\source\repos\CapeXPro\CapeXPro\Views\FAWizard_AddAsset.cshtml(43): error CS1579: foreach statement cannot operate on variables of type 'FAInfo' because 'FAInfo' does not contain a public definition for 'GetEnumerator''

抛出此错误是因为在您的局部视图FAWizard_AddAsset 中,您正试图像@foreach (DAL.Entities.FAInfo asset in Model) 那样迭代您的模型。这假设ModelFAInfo 的可枚举,就像List&lt;FAInfo&gt; 但显然它是FAInfo

根据您的代码,我认为没有FAInfo 的倍数,但在您的FAInfo 中有一个List&lt;ListStep2&gt;。如果您想对其进行迭代,请将您的 @foreach 更改为:

@foreach (DAL.Entities.Step2 asset in Model.ListStep2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多