Correct value still within View

0

I would like to know how I can change the value of @Model still in the view, before sending it to the server and which is the best way to do this?

Example:

   @{

       var model = (Pessoa)Model

       bool isAtivo = pessoa.IsAtivo
   }

   @Html.TextBox("Ativo", @isAtivo, new{...})

   Alterar

   Salvar

And if my item is a string how should it be done?

Example 2:

   @{

       var model = (Pessoa)Model

       string nome = pessoa.Nome
   }

   @Html.TextBox("Ativo", @nome, new{...})

   Alterar

   Salvar
    
asked by anonymous 30.03.2017 / 17:42

1 answer

2

Using the good old <form> :

@Html.BeginForm() { ... }

Because it is a collection of records, it is good to use the BeginCollectionItem package . With it, each of these objects can be represented by a part of your form.

Suppose your model is a collection or enumeration of objects:

@model IEnumerable<Objeto>

You need to write a form for it and iterate the records in order to create all the fields. For example:

@using (Html.BeginForm())
{
    foreach (var objeto in Model)
    {
        @Html.Partial("_PartialFormularioObjeto", objeto)
    }

    <input type="submit" value="Enviar" />
}

Partial would have:

@model Objeto

@using (Html.BeginCollectionItem("Objetos"))
{
    // Coloque aqui os campos do objeto.
}

Controller , in turn, would receive:

[HttpPost]
public ActionResult Enviar(IEnumerable<Objeto> Objetos)
{ ... }
    
30.03.2017 / 18:31