How to assign the value of a Tempdata["Test"] or Viewbag.Id to an html input?

Asked

Viewed 881 times

0

I wanted to know how to take the value of my variable tempData and put in an html input, in value maybe... someone could help me?

Code below:

<div class="container-fluid">
    <form method="post" style="margin: 0 auto; padding: 25px; background-color:white;">
        <input type="hidden" name="DataHora" id="DataVotacao" value="" readonly />
        <input type="text" name="IdFuncionario" id="DataVotacao" value="@(ViewBag.Funcionario)" readonly />
        <div>
            <label for="Senha">Recurso:</label>
            @Html.DropDownList("IdRecurso", (IEnumerable<SelectListItem>)ViewBag.Recurso)
        </div>
        <div>
            <label for="Comentario">Comentário(obrigatório):</label>
            <textarea name="Comentario" required="required"></textarea>
        </div>
        <div>
            <button type="submit" name="ConfirmaPassword" class="btn-primary btn-block" style="position:center">Cadastrar</button>
        </div>
    </form>
</div>
  • If you have the code?

  • If you’re saying Idfuncionaio?

  • Exactly... I want to put in that input, idFunctioning, the numerical value of Id that I have allocated in my Viewbag.Functioning. Or if I should store the Value in Tempdata to use there

  • It worked Thiago the answer?

  • It did, but there’s a problem.. Viewdata and Viewbag get lost after 1 redirect, right?

  • It depends what you’re doing, I posted the example, but, it seems that your reality is different, the big problem of the questions are those are never complete, but for what you asked is what I posted! got it

  • as much as the situation has a somewhat specific purpose, it served to clarify something yes and I am very grateful inclusive!

Show 2 more comments

1 answer

0


In his controller and method use ViewData, example:

public IActionResult Index()
{
    ViewData["IdFuncionario"] = 1;
    return View();
}

and in the View respective:

<input type="text" value="@(ViewData["IdFuncionario"])" name="IdFuncionario" />

is a way of passing information from controller for your View, but, not recommended, the correct thing is to work with Viewmodel.

Examples

In that link explains the difference between Viewbag, Viewdata and Tempdata, at that link and on that other link

Browser other questions tagged

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