0

After trying with a more sophisticated viewmodel with no success I came to use a viewmodel which is like this:

public class CarteExpressViewModel
{
    public string[] LesEntrees;      
    public string[] LesPlats; 

    public CarteExpressViewModel()
    {
        LesEntrees = new string[]{ "", "", "", "", "", "" };
        LesPlats = new string[]{ "", "", "", "", "", "" };
    }
}

I pass the viewModel in my create method of my controller

public ActionResult Create()
{
    CarteExpressViewModel carteExpressViewModel = new CarteExpressViewModel();
    return View(carteExpressViewModel);
}

Problem is I can see the input value if I use a FormCollection as parameter of my Post create method but everything is null if i use the viewmodel My view is line this:

@for (int i = 0; i < Model.LesEntrees.Length; i++)
{

    <div class="editor-label ">
        @Html.LabelFor(model => model.LesEntrees[i], "Entrée n°" + (i + 1).ToString(), new { @class = " express_input_label" })
    </div>
    <div class="editor-field  ">
        @Html.EditorFor(model => model.LesEntrees[i], new { @class = " express-input" })
    </div>
}

I can't see what's wrong but sure something should.

2 Answers 2

1

You set up the variables as fields, not properties. Change them to:

public string[] LesEntrees { get; set; }
public string[] LesPlats { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

0

Further to Chris Pratt answer, yes I should use properties but it's not enough since there is a problem with array (see http://forums.asp.net/t/1597890.aspx?Collection+is+read+only+Arrays+in+models) So I came back to my less simple viewmodel which use Lists and this one works.....

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.