-1

I am starting in asp.net Mvc and making test, i am doing a simple chat using PubNub api and i want finish it using only razor code only and one page only.

Model Chat.cs:

 namespace SimpleChat.Models
{
public class Chat
{

    public string NuevoMensaje { get; set; }
    public string TextArea { get; set; }
}
}

View:

   @model SimpleChat.Models.Chat

   @using (Html.BeginForm("Index","Chat",FormMethod.Post))
   {
   @Html.LabelFor(model => model.NuevoMensaje, "Nuevo Mensaje")
   @Html.TextBoxFor(model => model.NuevoMensaje) 
   <input type="submit"       class="btn-default" value="Enviar" />
   @Html.TextAreaFor(model => model.TextArea)
  }

Controller:

      static string variante = "";
    public ActionResult Index()
    {
        pubnub.Subscribe<string>("Chat", subCallback, connecCallBack, errorCallback);
        //Chat nuevochat = new Chat();
        return View();
    }


    [HttpPost]
    public ActionResult Index(Chat chat)
    {
        pubnub.Publish<string>("Chat", chat.NuevoMensaje, userCallback, puberror);
        chat.NuevoMensaje = "";
        chat.TextArea =variante;
        return View("Index",chat);
    }
    private void subCallback(string obj)
    {
        string[] retorno = obj.Split(',','"');
        variante += "Richard dice:" + retorno[0] + "\n";
    }

When i press submit don't get the new data, why?

toha
  • 5,095
  • 4
  • 40
  • 52
Richard Aguirre
  • 543
  • 6
  • 14
  • What 'new data' are you referring to? Do you mean that your expecting the textbox for `NuevoMensaje` to be reset to any empty string? –  Aug 18 '16 at 22:03
  • I edited the post, was incomplete check again please – Richard Aguirre Aug 18 '16 at 22:04
  • 1
    The comment stands. What are you expecting to happen and what does actually happen? –  Aug 18 '16 at 22:06
  • the static string variante is the pubnub call back with the sent messages – Richard Aguirre Aug 18 '16 at 22:06
  • Again. What are you expecting to happen and what is actually happening –  Aug 18 '16 at 22:07
  • i wan't the value of variante in the TextArea when i press submit and actually don't work, when i press submit i get nothing in the TextArea – Richard Aguirre Aug 18 '16 at 22:11
  • 1
    Add `ModelState.Clear();` before your reset the values. Refer the second part of [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) which explains the behavior –  Aug 18 '16 at 22:15

1 Answers1

1

If you want to render the updated value of TextArea property of your view model, You should clear the model state dictionary.

Model state dictionary has the initial values of your form inputs. So when razor (re)render the same view, It gives priority to the content in model state dictionary than the view model object you passed to the view.

You can use the ModelState.Clear() method to clear the model state dictionary values before returning to the view.

chat.TextArea = variante; 
ModelState.Clear();
return View("Index",chat);

Assuming your variante variable has the updated text.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks My Friend was really easy but i am like Jhon Snow in asp.net "i dont know nothing" jejejejeje. Now , My problem is PubNub the var "variante" dont get the new value at time if you test my code you will understand. – Richard Aguirre Aug 18 '16 at 23:22