Make if in an HTML tag to add or not attribute "checked"

Asked

Viewed 82 times

3

I’m developing with the .Net Core Razor and I’m setting a string in my GET and I need that, according to what this string (in the case of "true" or "false"), checkbox whether or not marked.

To string is being sent and received, but I am unable to handle the reply to mark or not this checkbox.

I’m trying to do it this way:

<input type="checkbox" id="dt_ini" name="checkDataInicial" onchange="liberarDataHora()" @HttpContext.Session.GetString("CheckDI") =="true" ? checked>

But this if What I’m doing isn’t working out like I want it to. What I want is that if @HttpContext.Session.GetString("CheckDI") is equal to true, add the attribute checked to that tag.

What is appearing:

inserir a descrição da imagem aqui Does anyone there have any idea how I can do?

EDIT: I managed to solve my problem but not as I would like. What I did was to change the String which is sent on GET. Where it was "true" I put "checked" and where it was "false" I put ""(empty); then my tag looked like this:

<input type="checkbox" id="dt_ini" name="checkDataInicial" onchange="liberarDataHora()" @HttpContext.Session.GetString("CheckDI")>

It worked, but it’s not quite how I’d like it. Anyway, if anyone there knows how to do this if, leave it there in the answer please.

1 answer

3


If I understand correctly, the Html.Raw() can help you:

@( suaVariavel == "true" ? Html.Raw("checked"): Html.Raw(""))
  • Can you show me how it turned out please?

  • 1

    I’m sorry, your answer is correct. It worked perfectly. I made the mistake. It seems that the code did not compile correctly when I upgraded to your code. Very good

Browser other questions tagged

You are not signed in. Login or sign up in order to post.