Tuesday, May 22, 2012

Get Value of Propperty ( List<long>) in Post Action at ASP.NET MVC3

This is My model:



    public class MyModel {
public List<long> NeededIds { get; set; }
public string Name { get; set; }
}


My Controllers:



public ActionResult Create() {

MyModel model = new MyModel();
model.NeededIds = new List<long> { 1, 2, 3, 4 };
return View(model);
}

[HttpPost]
public ActionResult Create(MyModel model) {

string name = model.Name;
List<long> ids = model.NeededIds;


return RedirectToAction("Index");
}


And View:



@model TestMVC.Models.MyModel

@using(Html.BeginForm()) {

<table>
<thead>
<tr>
<th>
Id
</th>
</tr>
</thead>
<tbody>
@foreach(long id in Model.NeededIds) {
<tr>
<td>
@id
</td>
</tr>
}
</tbody>
</table>

@Html.ValidationSummary(true)
<fieldset>
<legend>MyModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}


I set NeededIds in Get action and in view I can see NeededIds also I need it in Post action but in post action the NeededIds is always null, how can I get the property value in post action when i set it in get action? what is your suggestion?





1 comment: