English 中文(简体)
如何在 asp.net mvc 3 剃刀中显示模型财产?
原标题:How to get modelproperty displayed in asp.net mvc 3 razor?

我试图展示我模型上的一个属性。 此时我无法展示上面两个人的地址吗?

动作方法

public ActionResult Index()
{
    Person person = new Person() {name="Jan Kees",address="Beukenootjes straat 23" };
    Person person2 = new Person() { name = "Peter Jan", address = "Beukenootjes straat 23" };
    List<Person> lijst = new List<Person>();
    lijst.Add(person);
    lijst.Add(person2);

    return View(lijst);
}

人 人 类

 public class Person
 {
     public string name { get; set; }
     public string address { get; set; }
 }

索引视图

@model IEnumerable<stack.Models.Person>

@item.address @*how to display address?*@

@foreach (var item in Model)
{
    @item.name <br />
}
最佳回答

如果您想要只显示一次, 只需使用

@Model.First().address

我想在代码中添加更多解释,也许它也能帮助处于类似情况的其他人

此工作, 原因是您的模型是 IE 数不尽的, 所以要选择元素, 您可以使用 Linq, 并且 < code> First () 获得第一个序列元素

问题回答

您只需要将 < code item. address 调用到每个区块 :

@foreach (var item in Model)
{
    @item.name <br />
    @item.address
}

您可以按您需要的顺序订购,例如地址可以在姓名前出现。





相关问题
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. ...

热门标签