22

I am looking for solutions / suggestions on posting an string[]. My Model (Program), defines the target property (Levels) as a string. I can achieve this by just using FormCollection, but would prefer to pass that string[] right into the model from the post. Here are snippets:

ProgramConotroller/Create

//
// POST: /Program/Create
[HttpPost]
public ActionResult Create(Program obj)
{
    if(_service.CreateProgram(obj))
    {
        return RedirectToAction("Index");
    }
    // Add data to view data, remove if not necessary
    AddToViewData(obj);
    return View("Create", obj);
}

View/Create

...
<p>
<label>Program Levels</label>
<%= Html.ValidationMessageFor(model => model.Levels) %> <br />
<% foreach (Level level in (IEnumerable)ViewData["ProgramLevels"]) { %>
<input type="checkbox" name="Levels" value="<%= level.Id %>" <%= CommonExtensions.isChecked(level.Id.ToString(), Model.Levels) %> /><%= level.Name %><br />
<% } %>
</p>
...

1 Answer 1

36

Read about Model Binding to Arrays.

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

1 Comment

I should have know to check Scott's blog before posting... silly silly me.

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.