views:

337

answers:

1

Hello,

I'm having problems repopulating a TextBox from data coming from a complex structure involving Dictionaries.

So, I have VM structure like this:

public class AnswerVM
{
 public int Index { get; set; }
 public Answer Answer { get; set; }
 public Dictionary<int, QuestionResult> QuestionResults { get; set; }
}
public class QuestionResult
{
 public Dictionary<String, String> UserDefinedTextResult { get; set; }
}

And in my (partial) View, where my Model is an AnswerVM, I have something like this:

<%= Html.Hidden("QuestionResults[0].Key", Model.Answer.QuestionId)%>
<%= Html.Hidden("QuestionResults[0].Value.UserDefinedTextResult[0].Key", Model.Answer.Id)%>
<%= Html.TextBox("QuestionResults[0].Value.UserDefinedTextResult[0].Value") %>
<%= Html.TextBox("Index") %>

The DefaultDataBinder actually works with this, populating correctly my Model inside my Action (doing a regular Post):

public ActionResult Survey()
{
 // AnswerVMInSession is a AnswerVM from the Session :)
 UpdateModel(AnswerVMInSession);
 // Here my AnswerVMInSession does contains the String typed in the Textbox
 return View(AnswerVMInSession);
}

After I re show my View (with the partial view inside it), the (Dictionary) TextBox comes empty (but not the integer one), even though if a put a breakpoint in the View, just before rendering the TextBox, my model does contain the string in its structure.

Any ideas what's going on in here?

A: 

Duplicate of http://stackoverflow.com/questions/1133393/model-not-rendering-model-values-after-post

Brad Wilson
could be... I mean it's probably reflecting data from other object, but actually my ModelState is empty even after the UpdateModel call
murki
ok, given that it was an overly simplified example of mine, I see that for that case, ModelState.Clear() does seem to resolve the problem.
murki