Converting View Values to Controller

1

I need byte[]Descricao , when sent to my view , to display the text as string for the user to manipulate within TextArea , when I click in save I need to be converted back to byte[] to that Controller finds the submitted 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]
}

Would it be possible to validate via JavaScript perhaps within onClick of Save?

    
asked by anonymous 05.04.2017 / 03:03

1 answer

1

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

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);
        }
    }
}

Generate View with these changes that will result in this:

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 View format, analyzing this if all the mapping is correct and the array de bytes field has to be ignored in your model so that DescricaoString does not try to update this field, which is just a way to show and retrieve information:

Modifications to the Mapping of this class by adding:

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

References:

05.04.2017 / 16:49