主页包含基于用户角色的“子”视图

时间:2016-04-21 13:10:59

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

查看:

项目\视图\主页\ Index.cshtml

项目\领域\用户\视图\控制板\ Supervisor.cshtml

项目\领域\用户\视图\控制板\ Worker.cshtml

控制器:

项目\控制器\ HomeController.cs

当索引页面加载时,我想从HomeContoller调用一个动作来检测用户的角色并在索引页面内传递相应的视图。

Index.cshtml有这个一般设计:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

    <div class="everybodySees">
        <!-- some interesting stuff-->
    </div>
    <div id="target">
        <!-- some stuff based on user roles -->
    </div>

</body>
</html>

HomeController.cs

namespace MyApp.Controllers
{
    public class HomeController : ControllerBase
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult UserDashboard([DataSourceRequest] DataSourceRequest request)
        {
            ViewResult view;

            var emp = CommonInterfaces.Globals.UserDataSource.GetCurrentUser().GetRole();

            else if (emp == CommonInterfaces.Globals.Roles.Supervisor)
            {
                view = View("../Dashboard/Supervisor");
            }
            else 
            {
                view = View("../Dashboard/Worker");
            }

            return view;
        }
    }
}

Supervisor.cshtml

@model MyApp.Users.DashboardEmployeeWorkloadInfo

<div class="row-fluid">
    <div class="span12">
            @(Html.Kendo().Chart<MyApp.Users.DashboardUnitWorkloadInfo>().Name("TestChart")
        .DataSource(d => d.Read(r => r.Action("TestChart", "Dashboard")))
        .Legend(legend => legend.Position(ChartLegendPosition.Bottom))
        .Series(series => 
                {
                    series.Column(model => model.EmpName).Name("Workers in my Unit");
                })
                .ValueAxis(axis => axis.Numeric()
                    .MajorUnit(1)
                    .Line(line => line.Visible(false)))
            )
    </div>
</div>

Worker.cshtml

<article>
    <p>
       Area is static for now.
    </p>
</article>

我尝试在Index.cshtml页面的#target标记中使用@ {Html.RenderAction(“UserDashboard”,“Home”);}但是获取“执行处理程序的子请求时出错'System.Web.Mvc .HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper”。“

我对mvc很新,并想知道在MVC中是否有更好的方法来实现这一点。

1 个答案:

答案 0 :(得分:0)

如果您可以将用户当前的角色添加到View Model中,那么您可以在Index.cshtml中完成此操作

     local_filename:= 'D:/Capture.jpg';
{$ENDIF}
  RESTResponseDataSetAdapter.AutoUpdate :=false;
  RESTRequest.Params.Clear;
RESTRequest.Method := TRESTRequestMethod.rmPOST;
RESTRequest.AddParameter('application/json; charset=utf-8','{"title": "Capture.jpg"}',TRESTRequestParameterKind.pkREQUESTBODY);

  RESTClient.BaseURL := 'https://www.googleapis.com/upload/drive/v2';
  RESTRequest.Resource := '/files?uploadType=multipart';

  upload_stream := TFileStream.Create(local_filename,fmOpenRead);
  upload_stream.Position := 0
 RESTRequest.Addbody(upload_stream, TRESTContentType.ctIMAGE_JPEG);
RESTRequest.Execute;

这是假设,视图部分是独占的,即用户将看到任一视图。如果想法是用户首先应该看到常见的东西,然后只看到细节,那么你也可以做这样的事情

@if(Model.Emp == CommonInterfaces.Globals.Roles.Supervisor)
{
   <div id="target">
    <!-- some stuff based on user roles -->
</div>
}
else {
<div class="everybodySees">
    <!-- some interesting stuff-->
</div>

}

此外,如果您将值传递给div目标的部分视图,那么您还需要将其传递到主视图模型中,然后再将其传递给