1

我正在尝试在 MVC 4 应用程序中构建一个视图,该视图将显示域模型以及来自它的相关子实体的所有数据,但由于某种原因,我无法访问视图中的子级属性。Intellisense 看不到它们。

这是我的域模型,称为订阅者:

public class Subscriber
{

    [Required]
    [Display(Name = "Subscriber ID")]
    public int SubscriberId { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [Display(Name = "Date Created")]
    public DateTime DateCreated { get; set; }

    //set to money in DB
    [Display(Name = "Credits (Money owed to customer)")]
    private static decimal _Credits = 0;
    public decimal Credits { get { return _Credits; } set { _Credits = value; } }

    //set to money in DB
    [Display(Name = "Debits (Money owed to us)")]
    private static decimal _Debits = 0;
    public decimal Debits { get { return _Debits; } set { _Debits = value; } }

    public List<UserProfile> Users { get; set; }

    public List<Website> Websites { get; set; }

    public List<Order> Orders { get; set; }

    public List<SubscriberNote> SubscriberNotes { get; set; }

}

如您所见,订阅者有一堆相关的子实体。这是我使用急切加载方法的控制器代码:

public ActionResult Edit(int id = 0)
    { 
        var Subscriber = db.Subscribers
                           .Where(s => s.SubscriberId == id)
                           .Include(s => s.Users)
                           .Include(s => s.SubscriberNotes)
                           .Include(s => s.Orders)
                           .Include(s => s.Websites.Select(w => w.DomainNames))
                           .FirstOrDefault();

        return View(Subscriber);
    }

Ans 这是我的控制器:

@model PpModels.Models.Subscriber

@{
ViewBag.Title = "Edit";
}


@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>Subscriber</legend>

    @Html.HiddenFor(model => model.SubscriberId)


    --deleted fro brevity--

    --Problem is below--

    <div class="editor-label">
        @Html.LabelFor(model => model.Users.FirstName)
    </div>    

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

MVC 不喜欢 model.Users.FirstName。如何访问这些值?非常感激!

4

1 回答 1

0

您将不得不遍历此属性,因为它是一个List. Users将没有该FirstName属性,但其中的每个用户都会。

@foreach(var user in model.Users)
{
    <div class="editor-label">
        @Html.LabelFor(model => user.FirstName)
    </div>
    ....  
}
于 2013-03-25T23:16:40.913 回答