Fix value still inside the View

Asked

Viewed 57 times

0

I would like to know how I can change the value of @Model still in the view, before sending to the server and which 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 as it should be done?

Example 2:

   @{

       var model = (Pessoa)Model

       string nome = pessoa.Nome
   }

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

   Alterar

   Salvar
  • What is the purpose of this?

  • The example shown is only demonstrative.. because I actually have an object list where I want to change the (bool)status of the objects on my list without requesting the server for each change...

  • Ah, so come on. You have a list of objects on @Model. You want to change the status of some of her items. It is only on screen or the change has to reflect immediately on server?

  • First I want to modify everything on screen and then is forwarding my ready list to the server;

1 answer

2


Using the good old <form>:

@Html.BeginForm() { ... }

As it is a collection of records, it is good to use the package Begincollectionitem. 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" />
}

To Partial, in turn, would have:

@model Objeto

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

The Controller, in turn, would receive:

[HttpPost]
public ActionResult Enviar(IEnumerable<Objeto> Objetos)
{ ... }
  • I could not understand well how to change the value before going to the control?

  • If the status is a bool, you have to wear a @Html.CheckBoxFor() to represent it. Or a @Html.HiddenFor(), but I believe its intention is to show the information to the user.

  • But then it will change the bool value in Send() ?

  • If it’s like a form field, it can change. Remember I asked you what the point of this is? You haven’t talked yet.

Browser other questions tagged

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