0

I am wondering how this would work in MVC 2.

Suppose I want to render a view (Popup.ascx) that has a list of questions, I created these ViewModels

  public class VMPopup
  {
    public List<VMQuestion> Questions;
  }
  public class VMQuestion
  {
    public int Id
    public string Question;
    public string Answer;
    public bool Mandatory;
  }

I would have a method like this in the controller

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Popup(int elementId)
{
  List<VMQuestion> questions = new List<VMQuestion>();

  // Code to generate the questions
  // .....

  VMPopup vm = new VMPopup{Questions = questions};
  return View(vm);
}

1 - What would I put in the view Popup.ascx? Do I need a BeginForm here?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EnterpriseConnectMVC.Controllers.VMPopup>" %>

    <table border="1">
      <% foreach(var q in Model.Questions) { %>
        <%= Html.EditorFor(q); // I know this is wrong, how should I do it? %> 
      <% } %>
    </table>

    <input type="submit" value="OK" />

This is my view for VMQuestion

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EnterpriseConnectMVC.Controllers.VMQuestion>" %>

<tr>
  <td><%= Model.Question %></td>
  <td>
    <%= Html.TextBoxFor(m=>m.Answer) %>
    <%= Html.ValidationMessageFor(m=>m.Answer) %>
  </td>
</tr>

2 - Then how do I get the values back when the user hit the submit button?

Thanks in advance.

1 Answer 1

1

I'd accept a parameter of IEnumerable<VMQuestion> on the POST action.

You will need to add an index to the properties of each VMQuestion for the default model binder to bind the collection. See this article: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.