For example when using a

@Html.TextBoxFor(v => Model.Text);

and submitting the form, the value in the TextBox is automatically assigned to Model.Text.

I want to write such a custom HTML helper.

I know how to write such a extension method for displaying HTML, but how to program the behaviour to assign the value Model.WhatEver.

I want to build a Tag-Cloud and I think it is a good idea to include it in a way like:

@Html.TagCloud(v => Model.SelectedTags, Model.AvailableTags)

The HTML-Helper just prints out HTML, but how can I assign values to Model.SelectedTags?

有帮助吗?

解决方案

See how this method is defined:

public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression);

It is just generic extension method in static class returning MvcHtmlString. Just write one yourself and insert it into view via web.config.

<pages>
  <namespaces>
    <add namespace="NameSpaceWithYourClass" />

Remeber that this method have to take this HtmlHelper<TModel> as extension parameter and Expression<Func<TModel, TProperty>> expression as expression for selecting property and you will be fine.

BTW: i think for your need would be better partial view since HTML for it can be quite complicated.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top