【问题标题】:cannot convert lambda expression to type object because it is not a delegate type无法将 lambda 表达式转换为类型对象,因为它不是委托类型
【发布时间】:2020-05-21 09:30:36
【问题描述】:

我有一个从 Web API 返回 json 对象的 GET 方法。我已经创建了相应的模型。当我尝试将值放入视图时,出现此错误:

无法将 lambda 表达式转换为类型对象,因为它不是委托类型

我需要从模型中获取 location 属性的值。

控制器:

public IActionResult AddEditLoadBalancer(string subscriptionId, string resourceGroupName, string loadBalancerName, string subscriptionName)
{
    try
    {
        ViewBag.LBName = loadBalancerName;
        ViewBag.SubscriptionName = subscriptionName;
        var token = HttpContext.Session.GetString("Token");
        var tenantid = HttpContext.Session.GetString("TenantId");

        var sessionId = HttpContext.Session.GetString("SessionId");
        if (!string.IsNullOrEmpty(token) || !string.IsNullOrEmpty(tenantid))
        {
            var path = $"/api/PaasCatalog/AddEditLoadBalancer?subscriptionId=" + subscriptionId + "&resourceGroupName=" + resourceGroupName + "&loadBalancerName=" + loadBalancerName;
            var response = _httpClient.SendRequestWithBearerTokenAsync(HttpMethod.Get, path, null, token, tenantid, _cancellationToken, sessionId).Result;
            if (!response.IsSuccessStatusCode)
                return null;
            var result = response.Content.ReadAsStringAsync().Result;
            if (result == null)
                return null;
            var jsontemplates = JsonConvert.DeserializeObject<AddEditLoadBalancer>(result);

            return View(jsontemplates);
        }
        else
        {
            RedirectToAction("SignOut", "Session");
        }
    }
    catch (Exception ex)
    {
        _errorLogger.LogMessage(LogLevelInfo.Error, ex);
        return null;
    }
    return null;
}

查看:

<div class="form-group">
    <label class="col-md-2 control-label">Location:</label>
    <div class="col-md-4">
        @Html.EditorFor(x => x.location, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
    </div>
</div>

我哪里错了?请帮我。谢谢。

【问题讨论】:

  • 你没有提到你得到了什么错误!将您的问题添加到问题中。
  • ViewModel 是什么/在哪里?它有location 属性吗?

标签: c# model-view-controller razor lambda view


【解决方案1】:

代码中的一切都是正确的。我所要做的就是清理并重建整个应用程序并停止所有正在运行的工作进程。这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多