如何获得模型子模型属性? mvc asp.net

时间:2019-03-07 08:41:29

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

我有一堆这样的代码:

public class Customer : Person
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:MM:ss}")]
    [Display(Name = "Create Date")]
    public DateTime CreateDate { get; set; }
    public Status Status { get; set; }

    public virtual ICollection<Note> Notes { get; set; }
}

和我尝试致电时的另一个页面

    @model Demo.Models.Customer

    @Html.TextAreaFor(model => model.Notes.) 

我无法获得model.Note.Message

我想将其与客户绑定在同一页面中 当它显示客户详细信息并能够在同一页面中添加客户的注释时,我会尝试执行的操作。 任何想法?非常感谢

2 个答案:

答案 0 :(得分:1)

您的方法无效,因为您的 import UIKit class ViewController: UIViewController { @IBOutlet weak var textLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. textLabel.numberOfLines = 0 textLabel.lineBreakMode = .byWordWrapping let myShadow = NSShadow() myShadow.shadowBlurRadius = 3 myShadow.shadowOffset = CGSize(width: 3, height: 3) myShadow.shadowColor = UIColor.gray let attributedText = NSMutableAttributedString(string: "") let fatString = NSMutableAttributedString(string: "Bold", attributes: [NSAttributedString.Key.foregroundColor : UIColor.green, NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 16), NSAttributedString.Key.backgroundColor : UIColor.lightGray, NSAttributedString.Key.shadow : myShadow]) let normalString = NSAttributedString(string: "\n\nNormal", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]) let italicString = NSAttributedString(string: "\n\nItalic", attributes: [NSAttributedString.Key.foregroundColor : UIColor.yellow, NSAttributedString.Key.font : UIFont.italicSystemFont(ofSize: 16)]) let otherFontString = NSAttributedString(string: "\n\nBold + Italic", attributes: [NSAttributedString.Key.foregroundColor : UIColor.gray, NSAttributedString.Key.font : UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 16)]) let normalString2 = NSAttributedString(string: "\n\nUnderlined", attributes: [NSAttributedString.Key.underlineStyle : NSUnderlineStyle.single.rawValue, NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]) attributedText.append(fatString) attributedText.append(normalString) attributedText.append(italicString) attributedText.append(otherFontString) attributedText.append(normalString2) textLabel.attributedText = attributedText } } 是一个不包含model.Notes对象的集合。根据您的要求,您可以尝试Message或遍历model.Notes.First().Message并为每个便笺创建文本框。

model.Notes

答案 1 :(得分:0)

您可以尝试吗?

@foreach(var note in Model.Notes)
{
    @Html.TextArea(note.Message)
}