Reordering of Editor-For



  • Created the SharedPoint EditorTemplates - StringGeneralTemplate.cshtml. But some fields must be disabled and some are not. Create a new template because of one of the attributes of this stupid thing. Can we somehow send a request for additional attributes to my template? You mean, do it as an option?

    Example of my template:

    @model string
    

    <div class="row" style="padding-bottom: 3px;">
    <span class="td col-md-3"><label>@Html.ViewData.ModelMetadata.ShortDisplayName</label></span>
    <input class="form-control" type="text" name="@Html.ViewData.ModelMetadata.PropertyName" id="@Html.ViewData.ModelMetadata.PropertyName" value="@Html.ViewData.ModelMetadata.Model" />
    @Html.ValidationMessageFor(model => model)
    </div>



  • We can do a few ways.

    Option 1

    In method EditorFor Additional data (this is the third argument):

    @Html.EditorFor(x => x.TestProperty, "StringGeneralTemplate", new { disabled = true });
    

    In the template, these data are extracted through the dictionary. ViewData and processed:

    @model string
    

    @{
    var disabled = ViewData["disabled"] as bool?;

    var attributes = new Dictionary&lt;string, object&gt; {
        { "class", "form-control" }
    };
    
    // если дополнительные данные переданы
    if (disabled.HasValue &amp;&amp; disabled.Value) {
        attributes.Add("disabled", "disabled");
    }
    

    }

    // ...

    // Создаем input для свойства модели с нужными атрибутами
    @Html.TextBox(ViewData.ModelMetadata.PropertyName, Model, attributes)
    @Html.ValidationMessageFor(model => model)

    Option 2

    If the fact that the field is to be deactivated is known at the compilation stage, additional data can be transferred to the attribution AdditionalMetadata Model:

    public class MyModel
    {
    [DisplayName("Test property")]
    [AdditionalMetadata("disabled", true)]
    public string TestProperty { get; set; }

    // ...

    In the template, additional data are obtained through ModelMetadata.AdditionalValues:

    bool? disabled = null;
    if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("disabled")) {
    disabled = ViewData.ModelMetadata.AdditionalValues["disabled"] as bool?;
    }

    // Остальной код аналогичен предыдущему варианту




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2