ASP NET MVC5 - How to enable and disable HTML elements

Asked

Viewed 1,365 times

3

The program has in the session some customer data, as CPF and Full address (including state where you live). If the customer is from São Paulo, enable a specific combo, if it is from another state, not enable. In this case, then, I have to check the session if the state is São Paulo. The question is: The only way to do this is, check by code and return an ajax pro javascript to disable there or have I do everything in C#?

In webforms I had the controls that allowed me to do this in C#, but I don’t know how I do it in MVC since the only one that has access to HTML elements is javascript.

Way I’m doing now:

public JsonResult HabilitaCombo()
{
    if (Session["_CliEstado"].ToString() == "São Paulo") {
        return Json(new {habilita = true });
    }
    else
    {
        return Json(new { habilita = false});
    }
}

2 answers

5


Utilize ViewBag, to pass values of your methods to your views. It can also be ViewData.

In the method:

public ActionResult Grafico()
{
    ViewBag._CliEstado = "São Paulo";
}

Na View:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Grafico</title>
</head>
<body>
    <div>             
        @if (ViewBag._CliEstado != null && ViewBag._CliEstado == "São Paulo")
        {
            <input type="text" id="txtAparecer" name="txtAparecer" />
        }
    </div>
</body>
</html> 
  • 1

    Aaah now it’s clear! Thank you!

3

And if Voce used Viewbag?

Use that on the controller:

ViewBag.ClienteSP = VerificaClienteSP();

In cshtml:

if(@ViewBag.ClienteSP)
{
//habilita ou desabilita os componentes
}
  • Gee, I’ll try, thanks!

Browser other questions tagged

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