I would like to know know if I am doing an editor template correctly. And If I am doing it incorrectly, can you point me in the right direction to do it properly.
My Editor Template for a textarea:
@modeltype IEnumerable(Of be_PostTag)
@code
If Model Is Nothing Then
@Html.TextArea("PostTags")
Else
Dim sb As New StringBuilder
For Each x In Model
Dim tags = x.Tag & IIf(x.Equals(Model.Last), "", ", ")
sb.Append(tags)
Next
@Html.TextArea("PostTags", sb.ToString, 10, 50, Nothing)
End If
End Code
Edit: I see various posts on SO that suggest using the TextAreaFor helper instead of the non-For control(s) so that one can take advantage of model binding. But when I try to use TextAreFor, VS2013 complains that it is not accessible. I will need to post the PostTags back to the server and save them to the database when a post is made or edited. Hope this helps.
The guidance to use the Html.*For
style helpers is so that you can stay strongly-typed. If you use something like Html.TextBox("Foo")
, and later get rid of or change the name of your Foo
property, the view will happily keep rendering the input for Foo
, regardless. But, if you use Html.TextBoxFor(Function(x) x.Foo)
, then Intellisense will show you there's a problem.
However, the For
helpers, don't tend to play well with editor templates, simply because the context of what's the "model" changes inside an editor template. You're now working with just the property, not the complete model your view had. Here it's actually better to use the non-For
helpers and pass an empty string as the property name, i.e.:
@Html.TextBox("")
Razor will contextually fill the right value for you.
In your particular scenario here, though. An editor template is a poor choice. First, they're not really meant to contend with enumerables. You can use Html.EditorFor
on an enumerable, but what it really does is inspect the inner type and render an editor template for that type for each item in the enumerable, not one template for the entire enumerable property, itself. Also, as Stephen Muecke pointed out in the comments, it would be far better to combine the string, first, and then pass that combined string to your view. This is where view models come in handy, because you can create something like the following:
Public Class FooViewModel
Public Property Tags As IEnumerable(Of be_PostTag)
Public Property TagsAsString As String
Get
Return String.Join(",", Tags.Select(Function(x) x.Tag))
End Get
Set(ByVal Value As String)
Tags = Value.Split(",").Select(Function(x) New be_PostTag With { .Tag = x })
End Set
End Property
End Class
Then, in your view:
@Html.TextBoxFor(Function (x) x.TagsAsString)
A few caveats. I'm a C# guy, so take that code with a grain of salt. For what it's worth, VB.NET is dropping in popularity fast, and even more so in conjunction with ASP.NET MVC. For your own edification, I'd recommend picking up C#, and especially when you have problems, you'll find better and more helpful answers as the pool of ASP.NET MVC C# developers that can help you is much larger.