开发者

c# razor mvc textboxfor in foreach loop

how is it possible t开发者_如何学JAVAo retrieve data from TextBoxFor helper within a foreach loop? I mean:

in the view:

foreach(Language l in ViewBag.Languages){
    <td>@l.lang</td>
    <td>@Html.TextBoxFor(model => model.Name)
}

and how can I retrieve it in the controller once posted?

MyModel.Name //this returns the value of the first textbox within the foreach loop

By the way model.Name is defined in MyModel.cs as:

public string Name { get; set; }


You should be able to use the ModelBinder in your action by using:

public ActionResult MyAction(string[] name)
{
    foreach (var item in name)
    {
        // Process items
    }
}

Where name is the name automatically given to the text-boxes by Html.TextBoxFor().


Edit: If you wish to change the parameter name from name to something more descriptive, you can achieve this by using Html.TextBox, albeit with a loss of stong-typing:

@Html.TextBox("SomeMoreDescriptiveName", Model.Name);

And then in your controller action:

public ActionResult MyAction(string[] SomeMoreDescriptiveName)
{
    ...
}


@foreach (var _item in Model.ListSurvey)
{
    @Html.TextBoxFor(m=>m.Question, new { Value = @_item.Question })

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜