Convert View to Controller Values

Asked

Viewed 79 times

1

I need you to byte[]Descricao, when sent to my view displays the text as string for the user to manipulate within the TextArea, when I click in saving need to be converted again to byte[] for that the Controller find the sent template.

Class:

public class Carta {
    public virtual int Id {get; set;}
    public virtual byte[] Descricao {get; set;}
}

Controller:

public ActionResult MinhaCarta(int? Id){
     var carta = Id != 0 ? sessao.GetCarta(Id) : new Carta();

     View(carta);
}

public ActionResult SalvarCarta(Carta model){
     ...
}

View:

@model Carta
@{
   ViewBag.Title = "Minha Carta"; 
}


@using ((Ajax.BeginForm("SalvarCarta", "Mensagem",  new AjaxOptions { HttpMethod = "POST" })))
{
    @Html.Hidden(m => m.Id)
    @Html.TextAreaFor(m => m.Descricao)

    [SALVAR]
}

It would be possible to validate via Javascript maybe, within the onClick of Save?

  • You can create an attribute string in Model and already send the converted data to the View, and when post, convert again to byte.

  • Why are you doing this? You have some obligation!

  • Yes, I am trying to use the company standard, but the material I have has no example of how to do it @Virgilionovic

  • You save with ORM?

  • 1

    I use Nhibernate, if I’m not mistaken you mean a right ORM? @Virgilionovic

  • Worked?.....

  • Not yet, I get the idea but you’re having trouble putting this in my class. Because it does not let me define the "_Description" without the virtual one and if I put it virtual it says that the "_Description" is not valid for the kk item.. I’m trying to figure out a way to make it work.. @Virgilionovic

  • there’s something wrong because _descricao does not need this modifier!

Show 3 more comments

1 answer

1


Do the following then, in your class Carta modify, so that when the data is received by the user screen it assigns in the array de bytes automatically the values and when the ORM assign the value to the array de bytes it generates the information in text to be shown in View:

Class:

public class Carta
{
    public virtual int Id { get; set; }

    private byte[] _descricao;
    public virtual byte[] Descricao
    {
        get
        {
            return _descricao;
        }
        set
        {
            _descricao = value;
            _descricaoString =
                System.Text.Encoding.UTF8.GetString(value);
        }
    }

    private String _descricaoString;
    public string DescricaoString
    {
        get
        {   
            return _descricaoString;
        }
        set
        {
            _descricaoString = value;
            Descricao = 
                System.Text.Encoding.UTF8.GetBytes(value);
        }
    }
}

manages the Viewwith these changes that will result in:

View:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Carta</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.DescricaoString)
            <div class="col-md-10">
                @Html.EditorFor(model => model.DescricaoString)
                @Html.ValidationMessageFor(model => model.DescricaoString, "")
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

with these modifications the data is already ready for View and any attempt to change the data is converted to the format of array de bytes, analyzing this if the whole mapping is correct and the field DescricaoString has to be ignored in its model, so that the ORM do not try to update this field which is only even a way to show and retrieve information:

Modifications to the Mapping of this class by adding:

map.IgnoreProperty(p => p.DescricaoString);

References:

  • 1

    Thank you, it worked perfectly. however I had a small problem to make it work but I researched a little and found the answer, as I am using Nhibernate on startup of my Sesssion was asking to declare the 'virtual' in private byte[] _descricao.&#So I found the code : Here @Michaelgg . I just added the following command to Fluently: .ExposeConfiguration(x => x.Properties.Add("use_proxy_validator", "false")). And Ready, working perfectly!

Browser other questions tagged

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