ASP NET MVC5 - How to enable and disable HTML elements

3

The program has some customer data in the session, such as the CPF and Full Address (including the state where you live). If the client is from São Paulo, it enables a specific combo, if it is from another state, it does not enable it. In that 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 to check the code and return a javascript ajax to disable it there or how can I do everything in C #?

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

What 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});
    }
}
    
asked by anonymous 20.06.2014 / 22:04

2 answers

4

Use ViewBag , to pass values from your methods for your views. It can also be ViewData .

In method:

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

In 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> 
    
20.06.2014 / 22:18
3

What if you used ViewBag?

Use this in the controller:

ViewBag.ClienteSP = VerificaClienteSP();

No cshtml:

if(@ViewBag.ClienteSP)
{
//habilita ou desabilita os componentes
}
    
20.06.2014 / 22:11