【问题标题】:How to call the view in Url.Action如何在 Url.Action 中调用视图
【发布时间】:2020-05-14 20:47:39
【问题描述】:

我有一个从 web api 返回 json 数据的 GET 方法。我创建了一个相应的视图,但它显示 json 数据而不是在视图页面上显示数据。我的代码如下:

[HttpGet]
    public List<Project.Entity.ViewModels.PassCatalog.LBFrontendIPConfig> ListofLBFronendIPConfig(string resourceGroupName, string loadBalancerName)
    {
        try
        {
            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/GetLBFrontendIPConfigList?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<List<Project.Entity.ViewModels.PassCatalog.LBFrontendIPConfig>>(result);
                return jsontemplates;
            }
            else
            {
                RedirectToAction("SignOut", "Session");
            }
        }
        catch (Exception ex)
        {
            _errorLogger.LogMessage(LogLevelInfo.Error, ex);
            return null;
        }
        return null;
    }

这是我使用 Url.Action 调用 View 的方式

<i onclick="location.href='@Url.Action("ListofLBFronendIPConfig", "PaasCatalog",  new {LoadBalancerName = item.Name, resourceGroupName = item.RGName})'" class="fa fa-expand @Model.ActionClass.Edit" style="color:green;font-size: 18px;" data-toggle="tooltip" data-placement="bottom" title="Scale Up/Down" data-original-title="Tooltip on bottom"></i>

我错过了什么?请帮我。谢谢。

【问题讨论】:

    标签: c# asp.net model-view-controller view url.action


    【解决方案1】:

    您的 MVC 操作应在签名中具有 ActionResult(或 Task,如果您使用异步)的返回值。所以你的menthod应该被定义为...

    [HttpGet]
    public ActionResult ListofLBFronendIPConfig(string resourceGroupName, string loadBalancerName)
    {
        // . . . 
    }
    

    然后,您需要使用 action 方法更改您的 return 语句,以使用 MVC Controller 类中提供的 View Result 辅助方法。例如:

    [HttpGet]
    public ActionResult ListofLBFronendIPConfig( string resourceGroupName, string loadBalancerName )
    {
        try
        {
            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/GetLBFrontendIPConfigList?resourceGroupName=" + resourceGroupName + "&loadBalancerName=" + loadBalancerName;
                var response = _httpClient.SendRequestWithBearerTokenAsync( HttpMethod.Get, path, null, token, tenantid, _cancellationToken, sessionId ).Result;
                if ( !response.IsSuccessStatusCode )
                    return new HttpStatusCodeResult(response.StatusCode);  //return a status code result that is not 200. I'm guessing on the property name for status code.
    
                var result = response.Content.ReadAsStringAsync().Result;
                if ( result == null )
                    return View();  // this one could be lots of things... you could return a 404 (return new HttpNotFoundResult("what wasn't found")) or you could return a staus code for a Bad Request (400), or you could throw and exception.  I chose to return the view with no model object bound to it.
    
                var jsontemplates = JsonConvert.DeserializeObject<List<Project.Entity.ViewModels.PassCatalog.LBFrontendIPConfig>>( result );
                return View(jsontemplates);
            }
            else
            {
                // retrun the redirect result...don't just call it
                return RedirectToAction( "SignOut", "Session" );
            }
        }
        catch ( Exception ex )
        {
            _errorLogger.LogMessage( LogLevelInfo.Error, ex );
            // rethrow the exception (or throw something else, or return a status code of 500)
            throw;
        }
    }
    

    我更改了您的所有退货声明。您需要一个名为 ListofLBFronendIPConfig.cshtml 的 cshtml 视图,该视图知道如何使用绑定到它的模型的 json 对象。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2011-05-15
      • 2015-01-14
      • 1970-01-01
      • 2021-08-19
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多