Search code examples
asp.net-mvcasp.net-mvc-4razorrazor-2

How to assign model property to the additionalViewData in EditorFor in MVC4


The goal of my work is to disable the text box on a web page if condition1 is met.

So I created a view template called DoubleTemplate

@model double

@if (ViewData["IsVisible"] != null)
{
    var IsVisible = (bool)ViewData["Switcher"];

    if (IsVisible)
    { 
        @Html.TextBox(string.Empty,Model)
    }
}

And back to my web page, here is my code to call this template

@Html.EditorFor(m =>  m.Year1Data, "DoubleTemplate", new {Switcher = m.CurrentProgramYear == 1})

An error under the second m saying:

The name 'm' does not exist in the current context

So my question is how to assign my model property to the additionalViewData in EditorFor

Btw, here is the syntax of EnditorFor on MSDN

public static MvcHtmlString EditorFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    string templateName,
    Object additionalViewData
)

Solution

  • Assuming CurrentProgramYear is a property in you model, change

    .... new {Switcher = m.CurrentProgramYear ...
    

    to

    .... new {Switcher = Model.CurrentProgramYear ...