开发者

Submitting a List<T> of Items

开发者 https://www.devze.com 2023-04-05 04:50 出处:网络
Just a quick question from a newbie to the pro\'s. I am trying to implement a editable grid type form in my application. Here is the example.

Just a quick question from a newbie to the pro's.

I am trying to implement a editable grid type form in my application. Here is the example.

 @for (int i = 0; i < Model.Items.Count; i++)
    {
        <tr>
           开发者_C百科 <td>@Html.CheckBoxFor(m => m.Items[i].fav_ind)
            </td>
            <td>
                <a href="#" onclick="ShowDeals(@Model.Items[i].item_no);event.returnValue = false; return false;">
                    DEALS</a>
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].item_no)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].item_desc)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].mfr_item)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].pack_size)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].purc_uom)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].purc_uom_conv)
            </td>
            <td>@Html.DisplayFor(m => m.Items[i].list_prc)
            </td>
            <td>
                @Html.TextBoxFor(m => m.Items[i].nett_prc)
            </td>
            <td>
                @Html.TextBoxFor(m => m.Items[i].Qty)
            </td>
        </tr>
    }

This @for is inside a Html.BeginForm, because I want the user to be able to edit the last 2 fields (nett_prc and qty. There is also a submit bottom on the bottom of this grid(table).

Now for the question, when I submit, I get all the rows back in the controller, but on the items only the 2 fields where is is Html.TextBoxFor() has data in it. I want to get all the fields in the controller. I kwno I can use @Html.HiddenFor(), but I want to be able to display the other fields in labels, and when the user submits I want the values ass well.

Thanks in advance.


You can do both DisplayFor and HiddenFor at the same time:

<td>
    @Html.DisplayFor(m => m.Items[i].item_no)
    @Html.HiddenFor(m => m.Items[i].item_no)
</td>

However, why would you need HiddenFor? If the user is not supposed to edit some of the values, you can't trust the values that come from the form anyway. And if you can't trust them you will have to read the originals (e.g. from a database). What does HiddenFor buy you here?

0

精彩评论

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