Angular Directives in Html helper - c#

How to include Angular directives in Html Helper in ASP.NET MVC C#.
I dont know how to include it.
What I have done is following :
#Html.EditorFor(model => model.Questions.Question , new{ng_model="quest"})
{{quest}}
But when I look for source code that does not have ng-model directive?

The second Argument of Html.EditorFor is for the extended ViewData.
It is used by the template. The Default template dont use your additional entries.
But it supports htmlAttributes
#Html.EditorFor(model => model.Questions.Question , new { htmlAttributes = new { ng_model="quest" }})
or render it with out any template
#Html.TextBoxFor(model => model.Questions.Question, new { ng_model = "quest" })

To include Angular directives in Html Helper you should pass a null parameter as a second then pass the angular directive in a object.
#Html.DropDownList("ViewBag-Name", null, new { ng_model = "Model-Name"})
Where ViewBag-Name contains a SelectList object pass to the view from the controller.

We can do it very easily.
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #ng_change = "call()" ,#class="form-control" }})

#Html.TextBox("Text1", null, new { ng_model = "model1" })

Related

HTML Helper add custom attributes to checkboxFor

So Im currently writing a cypress test and would like to add a data-cy to checkbox's
But get an error on when im adding a data-cy="CheckBox"
#Html.CheckBoxFor(m => m.Steps, new { #class="form-check-input flex-shrink-0", #type="checkbox",name="BinComplaintRadios",#data-cy="CheckBox" })
#Html.CheckBoxFor(m => m.Steps, new { #class="form-check-input flex-shrink-0", #type="checkbox",name="BinComplaintRadios",#data_cy="CheckBox" })
Had to underscore data_cy instead of hyphenated

How to make `asp-for` to be aware of passed htmlAttributes? [duplicate]

I'm trying to use EditorFor custom templates.
I want to create a Int32 and decimal templates to render the inputs with some validations.
This is what I'm trying
#model int?
#Html.TextBoxFor(model => model, null, new { #type="text", #oninput = "this.value=this.value.replace(/[^0-9]/g,'')" } )
And I call it like
#Html.EditorFor(x => x.ExampleIntField)
It renders an <input type="text", oninput="this.value=this.value.replace(/[^0-9]/g,'')"
To here everything works, but when I try to pass extra htmlAttributes like readonly I don't understand how I must receive it in EditorFor template.
Example
#Html.EditorFor(x => x.ExampleIntField, new { htmlAttributes = new { #readonly = "readonly" } } )
I tried this I got the exact same <input type="text", oninput="this.value=this.value.replace(/[^0-9]/g,'')" rendered without readonly attribute
You are using the overload of EditorFor() that passes the object as additionalViewData. You can read that within the template from the ViewDataDictionary
#model int?
#{ var attributes = ViewData["htmlAttributes"]; } // returns { #readonly = "readonly" }
which you could then merge with your existing attributes and use in the TextBoxFor() method.
#{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(attributes);
htmlAttributes.Add("oninput", "this.value=this.value.replace(/[^0-9]/g,'')";
}
#Html.TextBoxFor(model => model, htmlAttributes)
Note that TextBoxFor() generates type="text" so there is no need to add it again. In addition, you do not need the leading # unless its a reserved keyword (for example #class = "...")

Input value does not show up in MVC

I am trying to populate textarea using the input value. But it is not working. Any solution to this issue?
#Html.EditorFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css", #Value = "ViewData.Model.ModelName" } })
You can remove quotes from #Value = "ViewData.Model.ModelName", because you are sending a string to Value property, not the content of ModelName property.
But why don't you use the TextArea helper? Like:
#Html.TextAreaFor(model => model.ModelName, new { htmlAttributes = new { #class = "textbox-css" })
It's not common set the value property like you are doing, I don't know if it works, actually. I think this is not a good way to do that. You should use the TextAreaFor(x => x.Property) or EditFor(x => x.Property) directly, instead of set the value property, as I said.
you don't need to manually populate the value property with the ViewData, remove the #Value = "ViewData.Model.ModelName". I think the problem is your ViewData name conflict with the property of the model (both have the name "ModelName").
Try changing the ViewData property name to something else. Works for me when I have an issue with unpopulated DropDownListFor

Add HtmlAttributes to Html EditorFor()

That is a tricky question because I want to add Html Attributes to the EditorFor() but do not want to replace the ones that was already created. Let me explain:
I have a default Editor Template for string.cshtml with the following code:
#{
var htmlAttributes = ViewData;
htmlAttributes["class"] = "text-box single-line form-control";
htmlAttributes["placeholder"] = ViewData.ModelMetadata.Watermark ?? ViewData.ModelMetadata.DisplayName;
htmlAttributes["title"] = ViewData.ModelMetadata.Watermark ?? ViewData.ModelMetadata.DisplayName;
}
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, htmlAttributes)
It is used to always add the following class, placeholder and title, based on the DisplayName DataAnnotation, for the Form Inputs, it is quite handy!
But the problem is that I'm having trouble adding the disable attribute for one specific Form Input with the common code:
#Html.EditorFor(x => x.Field, new { htmlAttributes = new { disabled = "" } })
When the Form Input is with a field that is not a string, it will not follow the created EditorTemplate and it will work with this exactly code, but when it is a string, the EditorTemplate replaces the Html Attributes.
Does anyone has any clue on this?
Turns out there was a few dumb errors, first of all the declaration on the EditorFor() field was wrong, the correct one is this:
#Html.EditorFor(x => x.Field, new { #disabled = "" })
The second point is to keep using the incoming htmlAttributes in the string.cshtml EditorTemplate, replacing the class property definition:
htmlAttributes["class"] = "text-box single-line form-control";
For:
htmlAttributes["class"] = htmlAttributes["class"] + "text-box single-line form-control";
In this way, the incoming html attributes is just concatenated with the new default ones.

Disabling EditorFor

I have a EditorFor HTML helper like this:
<td>#Html.EditorFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>
-
object disabledHtmlOptions = new { disabled = "disabled" };
I wanna make this disabled at every time. How do I do that? I dont want to do data annotations because this property is being used in other views too. Only on this view I want to disable it.
MVC 5.1 now allows passing in HTML attributes in EditorFor (see this answer). So you could do this:
#Html.EditorFor(model => m.Name, new { htmlAttributes = new { disabled = "disabled" } })
if you're stuck on using EditorFor you could set the disabled attribute via jQuery on page load. I know it's not ideal, but it's the only way unless you create an overload for EditorFor that accepts an htmlAttributes collection
$(document).ready(function() {
$('#Name').attr('disabled', 'disabled');
});
You could just render HTML for the view. If it's meant to be "read-only" just render the text. Otherwise you could render an <input> element.
For example, instead of
<td>#Html.EditorFor( m => m.Name )</td>
do
<td>#Model.Name</td>
or
<td><input type="text">#Model.Name</input></td>
I don't believe the signature of the EditorFor method allows you to specify any HTML attributes. You can if you change it to TextBoxFor however.
<td>#Html.TextBoxFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>

Categories

Resources