How to transform boolean input values to string values using C# (MVC)?

Asked

Viewed 57 times

2

I’m having trouble transforming values from a Toggle input into a string value to be stored in a BD. The goal is to make a Buy/Sell Toggle that shows the type of operation the user is entering the information, returning, in string, a "C" or "V" depending on the option:

Basicamente esse o botão

But I’m not getting the value of Toggle!

This is the code I’m doing to take what the button comes out and turn it into the strings

<script>
$(document).ready(function () {
        if ($("Tipo_ope").val().toLowerCase() === 'true')
            return "C"
        else
            return "V"
    }
 );
 </script>

I also tried for the Controller, but I can’t get the result by the parameter, and it didn’t work either... I am still beginner in this MVC method and still do not understand some things, so I ask if there is someone who knows how to solve this?

1 answer

1


You should not treat this in your frontend, the user can handle this function. Send in your form to the controller the value of the field:

$("Tipo_ope").val();

Obs: this selector may be wrong, remember to use the prefix "#" to select by ID: $("#Tipo_ope").val();

When your controller/service has the boolean value of this property, when instantiating the object before saving it in the database you can make a negotiation:

var objeto = new objeto() 
{
    tipoOperacao = tipoOpe ? "V" : "C",
};

Obs2: Wouldn’t it be more viewable to use the boolean in your database and instead of storing a char or a string, a flag, for example "Isvenda"?

  • would not be ideal... the field will be consulted in an Index and it would have to be specific. But thank you! He managed to clear up some things that he wasn’t getting

  • I get it. If my answer helped you clear your doubt, please mark it as accepted. Otherwise, comment on the remaining doubt so I can edit it.

Browser other questions tagged

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