Is it possible to replicate asp-for behavior? - c#

I created a view component that needs as a parameter the name of the input generated when using "asp-for".
For example this case:
<input type="text" class="form-control" asp-for="InputModel.Description" />
I need in the code behind to have a string with the value "InputModel.Description".
I tried using nameof() but that only returns "Description".
Any ideas?

Related

tag helper [asp-for] for the input doesn't work with byte's array values (opposite to #Html.HiddenFor)

When #Html.HiddenFor(e=>e.RowVersion) works well and generates:
<input id="RowVersion" name="RowVersion" type="hidden" value="AAAAAAAARlI=" />
But the tag helper version <input asp-for="#Model.RowVersion" name="RowVersion" hidden /> generates:
<input name="RowVersion" hidden id="RowVersion" value="System.Byte[]" />
Problem there is crazy value "System.Byte[]".
I want to keep using tag helper version for consistency. How I can enable bytes array serialization?
Use type="hidden" instead of hidden attribute!
You should be able to achieve the same thing if you do
/*
* From the ViewModel:
* byte[] RowVersion = Encoding.UTF8.GetBytes("FR")
*/
<input asp-for="RowVersion" type="hidden" />
Comparison
The reason (I am not 100% sure though)
Tag helper asp-for will try to generate the type for the HTML input based on the property type the tag helper binds to, when you don't specify a type attribute on the HTML input. If it can't find a proper type for the HTML input, it would default to type="text".
That's why your <input asp-for="RowVersion" hidden /> will generate a hidden textbox. Tag helper doesn't sanitize the input value when generating textbox:
But if you specify type="hidden" and your property type is byte[], it will actually do the Base64 encoding for you:
That's why #Html.HiddenFor() as well as <input type="hidden" asp-for= /> worked but the others didn't!

How to update ng-change/ ng-model using C# code?

I am using MSHTML to automate oen workflow involved Angular. I am able to add the value in text box but somehow underline function doesn't get call and hence I get unwanted result.
below is the sample:
objIHTMLElement = doc.getElementById("email");
objIHTMLElement.setAttribute("value", "sas#abc.com");
html looks as below:
<div class="inpt-fld-err-msg-inline">
<input title="Email Add"
class="ng-valid ng-valid-maxlength ng-touched ng-dirty ng-empty"
id="email" aria-invalid="false" type="text" maxlength="50"
ng-change="save(SearchParams)"
ng-model="SearchParams.userProfil" autocomplete="off">
</div>
is there any way to do it using C#?
Regards,
Ajit
You can register and call you javascript method by passing appropriate parameters via following syntax from C#:
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","save(" + SearchParams + )",true);
Kindly refer to this post: Calling JavaScript Function From CodeBehind

Why I can't show and edit double\floated values into this JQuery view?

I am very new in C#/.NET and JQuery view development and I am finding some difficulties trying to create input form to show and modify double values.
I have the following situation, into my view I have this input form:
<div class="ui-field-contain">
<label for="Severity">Severity:</label>
<input type="number" id="Severity" name="Severity" min="0" max="10" step=".1" value="#Model.Severity))" />
</div>
The value of the Severity field of my model object (#Model.Severity) is: 3.0 (I checked it using the debugger) but this value is not shown into my previous input tag.
The severity field is declared as double value into my model object, in this way:
public double? Severity { get; set; }
Why it can't correctly work?
I am thinking that maybe is a formattation\internazionalization problem because I see that other views use the , character for the floatted number (3,0 instead 3.0)
What canI do to fix this issue?
Tnx
Well thats because there is a extra closing angular braces ')' in the value which is being set to the severity fld
Replace
<input type="number" id="Severity" name="Severity" min="0" max="10" step=".1" value="#Model.Severity))" />
with
<input type="number" id="Severity" name="Severity" min="0" max="10" step=".1" value="#Model.Severity)" />
Happy Coding :)

Jquery validation for a name along with class [duplicate]

I have an HTML form that I'm processing with Perl CGI, like so:
<form action="process.cgi" id="main">
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
</form>
And I'm validating it with the jQuery Validation plugin. Trouble is, when I call $('#main').valid(), it only checks that the first field is non-empty. How to I get it to check all the fields?
OFFICIAL DOCUMENTATION:
jqueryvalidation.org/reference/#link-markup-recommendations
"Mandated: A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this. A 'name' attribute must also be unique to the form, as this is how the plugin keeps track of all input elements. However, each group of radio or checkbox elements will share the same 'name' since the value of this grouping represents a single piece of the form data."
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
<input type="text" name="foo" class="required" />
Quote OP:
Trouble is, when I call $('#main').valid(), it only checks that the
first field is non-empty. How to I get it to check all the fields?
It has nothing specifically to do with .valid() and it will fail the same using other jQuery Validate methods. That's because all three fields have the same name of foo. For this plugin each input must have a unique name.
DEMO: jsfiddle.net/xaFZj/
EDIT:
You cannot have duplicate names while using this plugin. This demo clearly shows that the plugin will fail when the name attribute is duplicated. There is no workaround since the name attribute is how the plugin keeps track of the form elements.
jsfiddle.net/ed3vxgmy/
The only exception is a radio or checkbox group where the elements in each grouping will share the same name.

How to pass values from HTML tags/elements to Code Behind in C#?

What's the best way to pass values from a HTML tag/elements to code behind without using the runat="server". Example
<input id="tags" type="text" />
Now, I want to pass the value of tags to code behind.
string tagsvalue = this.Request.Form.Get( "tags" );
I'm trying to use the code above but is not working. Any help will be really appreciate it.
You use the name attribute to specify the name in the name/value pair not the id
<input name="tags" id="tags" type="text" />

Categories

Resources