views:

55

answers:

2

Hello,

I've a problem with my viewmodel. I've a DropDownList with many translationvalues (pattern here)

In My controller - HTTPGET :

 public ActionResult Edit(int id) {

            int DropDownListValueId = id;

            SelectListViewModel viewmodel = new SelectListViewModel(0, DropDownListValueId);          

            return View(viewmodel);

        } 

In My view :

  <% using (Html.BeginForm())
       {%>
        <%: Html.ValidationSummary(true)%>

        <fieldset>
            <legend>Fields</legend>      

            <%foreach (var item in Model.DropDownListValue.DropDownListValue_Translation)
              {%>                  
                    <%: Html.TextBoxFor(model => item.Name) %><br />           

            <%  } %>

            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

In my controller, HTTPPOST :

[HttpPost]
public ActionResult Edit(SelectListViewModel viewmodel)
{                
    return View();
} 

My problem : data not passed in httppost, I think the problem is foreach in view??

Thank you,

Lore

A: 

Same problem Today :

In ViewModel :

 public class FormViewModel
    {

        public Customer Client { get; set; }
        public List<Customer> Contacts {get; set;}
}

In View (I send Contacts) :

<% using (Html.BeginForm()) { %>

<fieldset>
    <legend>Liste des contact</legend>
    <%foreach (var item in Model.Contacts)
      {%>
    Prénom :
    <%:Html.TextBoxFor(model => item.Firstname)%><br />
    Nom :
    <%:Html.TextBoxFor(model => item.Name)%><br />        
    <%} %>
</fieldset>

<input type="submit" />
<% } %>

In my controller, HTTPPOST :

   [HttpPost]
        public ActionResult Index(FormViewModel VM) {

            return View();

        }

My VM.Contacts = null!

How to pass a list of ViewModel to a controller?

There's something that I don't understand...

Akawan
A: 

I found the solution :

In my view, typed viewmodel :

<legend>Contacts</legend>
      <% for (int i = 0; i < Model.Contacts.Count; i++) { %>

            <%: Html.EditorFor(model => model.Contacts[i],"Contact") %>

        <% } %>

With this, I bind my list to controller!

Thank you all!!

Akawan