如何在父部分视图中访问模型数据

时间:2021-12-10 06:34:30

I have one controller which will pass data to view Index.cshtml

我有一个控制器,它将传递数据以查看Index.cshtml

public ActionResult Index()
    {
        var empId = @User.Identity.Name;
        var empDetails = empRepository.GetEmployeeDetails(empId);
        var emp = new UserViewModel {EmployeeDetail = empDetails};
        return View(emp);
    }

Layout of view Index.cshtml is Partialview _Layout.cshtml

视图的布局Index.cshtml是Partialview _Layout.cshtml

@model HRM.Areas.EmployeeDetails.Models.UserViewModel
@{
       ViewBag.Title = "Index";
       Layout = "~/Areas/EmployeeDetails/Views/Shared/_Layout.cshtml";
 }

 <div>Hello</div>

I want to access the data which I passed from controller action to Index.cshtml in _Layout.cshtml.

我想访问从控制器操作传递到_Layout.cshtml中的Index.cshtml的数据。

Is there any way to do this ?

有没有办法做到这一点?

3 个解决方案

#1


0  

Your question is not clear on how you want to access the values so i'm making an assumption here:

你的问题不明确你想如何访问这些值,所以我在这里做一个假设:

If you want to include data from a view in a rendering from your layout page you can use the RenderSection method.

如果要在布局页面的渲染中包含视图中的数据,可以使用RenderSection方法。

Layout Page:

@RenderSection("ViewSection", false)

View Page:

@section ViewSection {
<div>
    <label>Emp Name:</label>
    @model.EmpDetails.Name    
</div>

}

When your Layout is rendered it'll look for a section in your view that matches and render it. Passing a false as the 2nd param tell the Layout that the section is not required.

渲染Layout时,它会在视图中查找匹配并呈现它的部分。传递false作为第二个参数告诉布局该部分不是必需的。

For more info on this: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

有关这方面的更多信息:http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

#2


0  

Your question is not quite clear to me:/

您的问题对我来说不太清楚:/

My assumption -> If you change your code to look like this:

我的假设 - >如果您将代码更改为如下所示:

 public ActionResult Index()
        {
            var empId = @User.Identity.Name;
            var empDetails = empRepository.GetEmployeeDetails(empId);
            var emp = new UserViewModel { EmployeeDetail = empDetails };

            ViewBag.UserViewModel = emp;

            return View(emp);
        }

on the Layout page you can access your data:

在布局页面上,您可以访问您的数据:

@if (ViewBag.UserViewModel != null)
{
    //do sth with ViewBag.UserViewModel here
    //for example:
    @Html.Partial("_UserView",ViewBag.UserViewModel)

}

If you explain in details what you want to achive it will be easier for us to give you the right answer.

如果您详细解释您想要实现的目标,我们将更容易为您提供正确的答案。

#3


-1  

You can access the data in the model like this:

您可以像这样访问模型中的数据:

@Model.EmployeeDetail ...

#1


0  

Your question is not clear on how you want to access the values so i'm making an assumption here:

你的问题不明确你想如何访问这些值,所以我在这里做一个假设:

If you want to include data from a view in a rendering from your layout page you can use the RenderSection method.

如果要在布局页面的渲染中包含视图中的数据,可以使用RenderSection方法。

Layout Page:

@RenderSection("ViewSection", false)

View Page:

@section ViewSection {
<div>
    <label>Emp Name:</label>
    @model.EmpDetails.Name    
</div>

}

When your Layout is rendered it'll look for a section in your view that matches and render it. Passing a false as the 2nd param tell the Layout that the section is not required.

渲染Layout时,它会在视图中查找匹配并呈现它的部分。传递false作为第二个参数告诉布局该部分不是必需的。

For more info on this: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

有关这方面的更多信息:http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

#2


0  

Your question is not quite clear to me:/

您的问题对我来说不太清楚:/

My assumption -> If you change your code to look like this:

我的假设 - >如果您将代码更改为如下所示:

 public ActionResult Index()
        {
            var empId = @User.Identity.Name;
            var empDetails = empRepository.GetEmployeeDetails(empId);
            var emp = new UserViewModel { EmployeeDetail = empDetails };

            ViewBag.UserViewModel = emp;

            return View(emp);
        }

on the Layout page you can access your data:

在布局页面上,您可以访问您的数据:

@if (ViewBag.UserViewModel != null)
{
    //do sth with ViewBag.UserViewModel here
    //for example:
    @Html.Partial("_UserView",ViewBag.UserViewModel)

}

If you explain in details what you want to achive it will be easier for us to give you the right answer.

如果您详细解释您想要实现的目标,我们将更容易为您提供正确的答案。

#3


-1  

You can access the data in the model like this:

您可以像这样访问模型中的数据:

@Model.EmployeeDetail ...