English 中文(简体)
在另一种观点中,采用伙伴关系框架中的参数。 NET MVC
原标题:Reusing a view in another view with parameters in ASP.NET MVC
最佳回答

引诱书写代码,以为出面,并自取数据。 这包括利用诸如“性别行动”等内在职能。 即便是“放弃”执行另一控制人,也并不意味着认为自己没有采取行动,这或许会打破认为意见完全无效的多国公司做法,而该模式本应包含everything<>>m>。 这些观点需要。

相反,你可以做些什么是回到你发行的单页上的一个模式,其中包含一个包含问题清单的财产:

public class IssueListModel {
    public List<Issue> Issues { get; set; }
}

Populate it in your issue list action:

public ActionResult IssueList(string projectName) // projectName may be null
{
    var issueListModel = new IssueListModel();

    issueListModel.Issues = SomeRepository.GetIssues(projectName); // whatever you need to send...


    return View(issueListModel);
}

接着,在您的名单上,你可以通过:

@foreach (var issue in Model.Issues) {
    <div>@issue.IssueName</div>
}

或者,你可以将问题收集到部分观点:

@Html.RenderPartial("IssueList", Model.Issues)

您可以提出部分看法,期望你们能够把自己的观点推向你们。 清单作为模型:

@model List<MyProject.Models.Issue>

......然后从部分角度看待它,此时此就模型本身进行推论:

@foreach (var issue in Model) {
    <div>@issue.IssueName</div>
}

那么,你可以做的是,为您的项目细节观点提供一个单独的模式,其中还包括一个包含问题的财产:

public class ProjectDetailModel {
    public Project Project { get; set; }
    public List<Issue> Issues { get; set; }
    public string Whatever { get; set; }
}

在控制人员中,你可以履行你在名单上担任控制职务的相同职能:

public ActionResult ProjectDetail(string projectName)
{
    var projectDetailModel = new ProjectDetailModel();

    projectDetailModel.Issues = SomeRepository.GetIssues(projectName, 10); // whatever you need to send


    return View(projectDetailModel);
}

然后,你可以再次对你提出同样准确的部分看法。 项目详细观点:

@Html.RenderPartial("IssueList", Model.Issues)

答案是漫长的,但我希望这是你所期待的!

问题回答

If you want to re-use presentation logic only, you can use partial view. If you want to re-use also controller s logic, you have to use child action combined with partial view.

创建控制员

public class IssuesController : Controller
{
    [ChildActionOnly]
    public PartialViewResult List(string projectName, int issueCount = 0)
    {
        IEnumerable<Issue> issueList = new List<Issue>();

        // Here load appropriate issues into issueList

        return PartialView(issueList);
    }
}

不要忘记在问题夹中创造适当的部分观点。

最后在您的项目意见中采用这一行文。

@{ Html.RenderAction("List", "Issues", new { projectName = "Px", issueCount = 10 } ); }

目 录

@{ Html.RenderAction("List", "Issues", new { projectName = "Px" } ); }

在你的控制器方法中,这一观点被点名,而不是仅仅见观点。

ie...

public ViewResult IssueView1()
{ return View("Issue");}

public ViewResult IssueView2()
{return View("Issue");}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签