Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting an array in mvc form

Tags:

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>
...
like image 489
gnome Avatar asked Aug 06 '10 17:08

gnome


1 Answers

Read about Model Binding to Arrays.

like image 71
Yuriy Faktorovich Avatar answered Oct 18 '22 20:10

Yuriy Faktorovich