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>
...
Read about Model Binding to Arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With