从MVC3中的表单中检索数据

时间:2012-03-06 09:09:03

标签: c# asp.net asp.net-mvc asp.net-mvc-3 razor

我目前正在尝试从视图中的表单中获取数据到我的控制器,我正在向它发送一个对象列表,我的目标是通过具有多个表单来获取已修改的那个。这里的问题是我从视图中得不到任何东西,只是空值。

@model List<Connection_User>

@for (int i = 0; i < Model.Count ; i++)
{
using(Html.BeginForm("DBLogin","User", FormMethod.Post, new { autocomplete ="off"}))
{
    <fieldset>

    <div class="editor-label">
        @ConsoleV2.Resources.Strings.Name
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model[i].Username)
        @Html.ValidationMessageFor(model => model[i].Username)
    </div>

    <div class="editor-label">
        @ConsoleV2.Resources.Strings.Password
    </div>
    <div class="editor-field">
        @Html.PasswordFor(model => model[i].Password)
        @Html.ValidationMessageFor(model => model[i].Password)
    </div>   
    <p>
        <input type="submit" value="@ConsoleV2.Resources.Strings.Valid" />
    </p>
    </fieldset>
}
}

我控制器中方法的原型是

[HttpPost]
public ActionResult DBLogin(Connection_User Logins)
{


        return new EmptyResult();
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试使用foreach代替for

@foreach(var modelItem in Model)
{
using(Html.BeginForm("DBLogin","User", FormMethod.Post, new { autocomplete ="off"}))
{
    <fieldset>

    <div class="editor-label">
        @ConsoleV2.Resources.Strings.Name
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(item => modelItem.Username)
        @Html.ValidationMessageFor(item => modelItem.Username)
    </div>

    <div class="editor-label">
        @ConsoleV2.Resources.Strings.Password
    </div>
    <div class="editor-field">
        @Html.PasswordFor(item => modelItem.Password)
        @Html.ValidationMessageFor(item=> modelItem.Password)
    </div>   
    <p>
        <input type="submit" value="@ConsoleV2.Resources.Strings.Valid" />
    </p>
    </fieldset>
}
}