English 中文(简体)
我如何把物体汇往我的MVC3中的观点?
原标题:How can I pass a collection of objects to the view in my MVC3?

我采取了以下行动方法,收集了阿明尾矿:

   public ActionResult DetailData()
    {
        var vm = _reference.Detail().ToList();
        return PartialView("~/Areas/Administration/Views/References/_DetailData.cshtml", vm);
    }

这里是我的阿明细类:

namespace Storage.Models
{
    public class AdminDetail
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string Title { get; set; }
        public string Status { get; set; }
        public string Type { get; set; }
        public string Level { get; set; }
        public string Order { get; set; }
        public int Row { get; set; }
    }

在我的raz子中,我对这个模式应作什么? 如果我只是这样说,它只是一个例子:

@model Storage.Models.AdminDetail

    @foreach (var detail in Model.AdminDetails)
    {

我如何利用我的模型收集污染物? 我不敢肯定,如果把数据视为一种选择,但我会不使用,因为我听到数据不是真正的选择。

最佳回答

您可使用<代码>AdminDetail的收集作为你的观察模型,以便你能够通过下列内容:

@model IEnumerable<Storage.Models.AdminDetail>
@foreach (var detail in Model)
{
    <div>@detail.Title</div>
    ...
}

或者使用显示模板,你可以避免休息:

@model IEnumerable<Storage.Models.AdminDetail>
@Html.DisplayForModel()

在相应的显示模板中,将自动为收集的每一要素提供(< 代码>~/Views/ Common/DisplayTemplates/AdminDetail.cshtml):

@model Storage.Models.AdminDetail
<div>@Html.DisplayFor(x => x.Title)</div>
...
问题回答

你可以确定一个名单,作为你的模式——MVC3中的模式是你想要的。 仅将清单作为意见的范本。

Alternatively, use the ViewBag (although I would not recommend to abuse it as you loose IntelliSense). ViewBag is like a dynamically typed dictionary which is available from both the View and the Controller. Here s an example:

主计长:

ViewBag.Color = "Blue";

观点:

<h1> The color is <%: ViewBag.Color %> </h1>




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签