MVC #Html.Display - asp.net-mvc

I have something like:
<input type="text" name="TerrMng" id="TerrMng"/>
in HTML. What is the equivalent of the above using #Html.Display?
I tried using:
#Html.Display("TerrMng", TerrMng)
but was not successful. Note that I like to use #Html.Display but not sure how to translate the ID value so that it shows up.

This should do the trick for you. Adding the TerrMng as a 2nd parameter sets the value of the created html but the variable must come from your Model on load.
#Html.Display("TerrMng");

Related

adding an id attribute to q-input

Say I have the following q-input:
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"/>
I'd like to be able to make it so that if the domain of the email is mydomain.com that the form action will change to another website (without csrf protection) and the POST will be made to that website instead of the main one.
To do this I was thinking I could use jQuery. eg. $('#email').val().replace(/^.+#/, '') == 'mydomain.com' then change the form action and submit.
The only problem is: I don't know how to set an id attribute on q-input.
Any ideas?
As of early Quasar 1.4.2 (November of this year) you can specify the id value on the resulting html generated by q-input by using the "for" property (see the end of the behavior properties: https://quasar.dev/vue-components/input#QInput-API).
So, for example, you could add for="myInputId":
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"
for="myInputId"
/>
The id attribute with value "myInputField" will end up on the resulting <input> element in your HTML.
Not using the "for" in the elements gave me a lot of headaches because the Jest snapshot generated random IDs

knockout textarea value binding shows ASCII characters for line breaks?

I have a knockout value binded textarea like so:
<textarea class="form-control" data-bind="value: test" name="test" rows="4"></textarea>
However, when saving data with line breaks or extra spaces, upon next viewing of the page it displays it with the ASCII characters. Like so:
Overview being explained here in this box.
Test stuff test stuff.
When I look at the value in the database it does not have those characters:
Overview being explained here in this box.
Test stuff test stuff.
Indeed even in the view model before the data gets passed to the view everything looks correct:
Overview being explained here in this box.\n\nTest stuff test stuff.
And finally even when first entered, the knockout property pageModel().test() shows the text like this too:
"Overview being explained here in this box.
Test stuff test stuff."
So why does knockout finally render it with the ASCII characters? My KO model looks like this when the value is already input:
self.test = ko.observable('Overview being explained here in this box.
Test stuff test stuff.');
Okay, it's an MVC issue then, because I am calling it like so:
self.test = ko.observable('#Model.Test');
But I can't figure out how to get this to work, if I use Html.Raw the line breaks are interpreted correctly but then that breaks the javascript breaks because there is a space between lines without proper string ending.
I also try just escaping the \r\n characters:
self.test = ko.observable('Overview being explained here in this box.\\n\\nTest stuff test stuff.');
This doesn't remove the problem though, I still get the double
.
How the heck can I get this working right while still using the value binding? Because I am using knockout.validation, and I could make this work by using html binding and use a slight hack:
<textarea class="form-control" data-bind="value: test, event: { keyup: function(data, event) { data.test(event.target.value); } }" name="test" rows="2"></textarea>
But this completely breaks knockout validation for any textareas because html is a one way binding that doesn't get validated. So I still need to use the value binding.
How to get this working?
You're probably looking for
self.test = ko.observable('#HttpUtility.JavaScriptStringEncode(ViewBag.Test)');
or as you pointed out for .NET Core you'll need
#(JavaScriptEncoder.Default.Encode(question.Test))

Passing html object properties into embedded ruby code

How can I pass html object properties into embedded ruby code in an html.erb file?
Lets say I have a ruby method A that accepts a string parameter(and also the return value of A is string). I think of scenarios like the following:
<input type="text" id="t" value="Leaves">
<%= A(document.getElementById("t").value) %>
Obviously I can't write code that way.
I want to pass the value/text of the textbox into method A and print A's return value into the html body. How can I do that?
Also, if I want to continuously check the value of the textbox and append A's return value(when passed the current value of the textbox to A) to the body of the document, what should I do? And if I instead wanted to set some paragraph p's text to this return value, what should I have done?
You can use a HTML parser like Nokogiri.
frag = Nokogiri::HTML.fragment('<input type="text" id="t" value="Leaves">')
frag.at_css('#t').attr('value')
But it seems like a rather silly and overcomplicated solution to something that most likely can be solved by not using HTML strings to pass around data in your views / helpers in the first place.

thymeleaf: th:value is ignored when using th:field

I've got a form where I want to edit some user data.
So the already stored data is put as th:value and after sending I validate with spring validation and want to give back the form on wrong input. I want the input field to have the value of the input by the user but it always gives me the stored input instead.
That's how an input field looks like
<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/>
If the form is loaded the first time the input fields should have the value of the already stored data.
If it's loaded after submit and with an validation error, the input fields should have the value of the user's input.
Is there a way to to that?
Thanks!
Attribute th:field will replace attributes value, id and name in your input tag.
Instead, use plain th:id, th:value and th:name without using th:field. Then you will get what you wanted.
Then it will look like:
<input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/>
Similar answer is here: How to set thymeleaf th:field value from other variable
Because Attribute th:field is Combination of both th:name and th:value
So, either use this:
<input type="text" th:value="${product.name}" th:name="name" th:errorclass="fieldError"/>
Or this:
<input type="text" th:field="*{name}" "th:errorclass="fieldError"/>
Using th:field or value, id and name is ok. if you user th:field, you can write this:
<input type="text" th:field="${product.name}" th:value="${product.name}" "th:errorclass="fieldError"/>
You have error in your controller (you set wrong value to *{name}), if the input value is wrong after validation error.

How can I setup a link without exposing the link itself?

Actually its not actually a problem, but I'm thinking and searching for it for a while.
When we use php and to setup some link we can use something like-
some link
in this way anyone can see what is going on by looking at the url. Is there any possible way to keep it hidden like the POST method of the form element?
I don't want user to modify and play around with my parameters and values. Lets guess I don't want to encrypt the values and parameters and I can't use form element as they should be pure links. And also what if I don't want to use url rewriting engine.
Any ideas from the experts?
edit:
I forgot to mention another most important thing that I need to get the parameters and the values I want to pass through that link and do some stuffs in the page i'm linking to.
thanks again.
You can use a hidden form
<form action="realurl" method="post">
<input type="hidden" name="parameter" value="value1"></input>
</form>
<a onclick="forms[0].submit();" href="http://fake">link text</a>

Resources