开发者

Html Helper for IEnumerable<T> collection

I have two classes MyClassA and MyClassB:

public class MyClassA
{
    public int Age 开发者_如何学编程{ get; set; }
    public string Name { get; set; }
}

public class MyClassB
{
    public IEnumerable<MyClassA> Data { get; set; }
}

Now, I want to create custom strongly typed html helper to generate textboxes with names from collection MyClassA, something like this:

@model MyClassB

@Html.MyTextBoxFor(p => p.MapFrom(o => o.Age))
@Html.MyTextBoxFor(p => p.MapFrom(o => o.Name))

... with the output:

<input type="text" name="Age" />
<input type="text" name="Name" />

How can I accomplish this?


PS. I know, I can write sth like this:

@Html.TextBoxFor(p => p.Data.First().Name)

but it feels so wrong and inelegant...

Any ideas?


Let me make sure I understand this correctly... you want to create a textbox for Name and Age for each MyClassA in Data property of MyClassB. If so, then editor templates to the rescue.

Create /Views/Shared/EditorTemplates/MyClassA.cshtml

@model MyClassA

@Html.TextBoxFor(m => m.Name)
@Html.TextBoxFor(m => m.Age)

then in you view:

@model MyClassB

@Html.EditorFor(m => m.Data)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜