MVC格式不张贴日期表 [英] mvc form not posting table date

查看:103
本文介绍了MVC格式不张贴日期表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我失去了一些东西很明显,或者我不理解我至今读。我有一个从包含数据,2场,其中需要是可编辑的一个表。通过形式接收的数据是的IEnumerable 。但是,当控制器功能接收后的数据,而不是将其接收的IEnumerable ,我什么也没得到。如果我刚收到的原始数据类型,我获得对象的一个​​实例用正确的id字段和所有其他字段为空。可能有人请点我OIN朝着正确的方向?

MODEL:(由EF模型首先生成)

 部分公共类QuoteBundlePackage_Result
    公共属性ID作为整数
    公共财产雇员为可为空(整数)
    公共属性employeeName作为字符串
    物业bundleId为可为空(整数)
    公共属性bundleDescription作为字符串
    公共属性包标识为可为空(整数)
    公共属性packageContents作为字符串
末级

查看:

  @ModelType的IEnumerable(中gbip_new.QuoteBundlePackage_Result)@using Html.BeginForm()
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>&报价LT; /传说>
<表>
    &所述; TR>
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.employeeId)
        < /第i
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.employeeName)
        < /第i
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.bundleId)
        < /第i
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.bundleDescription)
        < /第i
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.packageId)
        < /第i
        <第i个
            @ Html.DisplayNameFor(功能(模型)model.packageContents)
        < /第i
        百分位>< /第i
    < / TR>    @For每个项目在模型
        昏暗的CURRENTITEM =项目
        Html.HiddenFor(功能(modelItem)currentItem.id)
        @< TR>
            &所述; TD>
                @ Html.DisplayFor(功能(modelItem)currentItem.employeeId)
            < / TD>
            &所述; TD>
                @ Html.DisplayFor(功能(modelItem)currentItem.employeeName)
            < / TD>
            &所述; TD>
                @ Html.EditorFor(功能(modelItem)currentItem.bundleId)
            < / TD>
            &所述; TD>
                @ Html.DisplayFor(功能(modelItem)currentItem.bundleDescription)
            < / TD>
            &所述; TD>
                @ Html.EditorFor(功能(modelItem)currentItem.packageId)
            < / TD>
            &所述; TD>
                @ Html.DisplayFor(功能(modelItem)currentItem.packageContents)
            < / TD>
        < / TR>
    下一个
< /表>
&所述p为H.;
    <输入ID =Submit1类型=提交值=提交/>
&所述; / P>
< /字段集>
使用完

控制器

 < HttpPost()> _
    功能QuoteBundlePackage(BYVAL eqDetails为IEnumerable(中Global.gbip_new.QuoteBundlePackage_Result))作为的ActionResult
        如果ModelState.IsValid然后            '做东西            返回RedirectToAction(指数)
        万一        返回查看(eqDetails)
    结束功能


解决方案

模式绑定到一个集合的作品有点不同。您需要有效的号码,你通过,如果你想你的收藏循环的每个项目进行正确绑定。

要呈现什么是一样的东西...

 <输入类型=文本名称=QuoteBundlePackage_Result [0] .EmployeeIDVALUE =/>
<输入类型=文本名称=QuoteBundlePackage_Result [0] .EmployeeNameVALUE =/><输入类型=文本名称=QuoteBundlePackage_Result [1] .EmployeeIDVALUE =/>
<输入类型=文本名称=QuoteBundlePackage_Result [1] .EmployeeNameVALUE =/>

...这将允许该框架的每个项目区分。要创建这样的安排,你应该给每件商品的循环中的ID - (对不起!事先在C#中,而不是vb的答案是)

  @for(INT I = 0; I< Model.Count();我++)
{
    @ Html.EditorFor(E => E [I] .EmployeeID)
    @ Html.EditorFor(E => E [I] .EmployeeName)
}

请参阅从<一相关文章href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\"相对=nofollow>斯科特Hansleman 和的菲尔哈克

I am sure I am missing something very obvious, or I am not understanding what I have read so far. I have a from that contains a table of data, 2 fields of which need to be editable. The data received by the form is IEnumerable. However, when the controller function receiving the post data, instead of it receiving an IEnumerable, I get nothing. If I receive just the raw data type, I get the a single instance of the object with the correct id field and all other fields are empty. Could someone please point me oin the right direction?

MODEL: (Generated by EF Model first)

Partial Public Class QuoteBundlePackage_Result
    Public Property id As Integer
    Public Property employeeId As Nullable(Of Integer)
    Public Property employeeName As String
    Property bundleId As Nullable(Of Integer)
    Public Property bundleDescription As String
    Public Property packageId As Nullable(Of Integer)
    Public Property packageContents As String
End Class

View:

@ModelType IEnumerable(Of gbip_new.QuoteBundlePackage_Result)

@Using Html.BeginForm()
@Html.ValidationSummary(True)
<fieldset>      
<legend>Quote</legend>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(Function(model) model.employeeId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.employeeName)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.bundleId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.bundleDescription)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.packageId)
        </th>
        <th>
            @Html.DisplayNameFor(Function(model) model.packageContents)
        </th>
        <th></th>
    </tr>

    @For Each item In Model
        Dim currentItem = item
        Html.HiddenFor(Function(modelItem) currentItem.id)
        @<tr>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.employeeId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.employeeName)
            </td>
            <td>
                @Html.EditorFor(Function(modelItem) currentItem.bundleId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.bundleDescription)
            </td>
            <td>
                @Html.EditorFor(Function(modelItem) currentItem.packageId)
            </td>
            <td>
                @Html.DisplayFor(Function(modelItem) currentItem.packageContents)
            </td>
        </tr>
    Next
</table>
<p>            
    <input id="Submit1" type="submit" value="submit" />
</p>
</fieldset>
End Using

Controller

    <HttpPost()> _
    Function QuoteBundlePackage(ByVal eqDetails As IEnumerable(Of Global.gbip_new.QuoteBundlePackage_Result)) As ActionResult
        If ModelState.IsValid Then

            'Do stuff

            Return RedirectToAction("Index")
        End If

        Return View(eqDetails)
    End Function

解决方案

Model binding to a collection works a little differently. You need to effectively number each item you are looping through if you want your collection to be bound correctly.

What you want to render is something like...

<input type="text" name="QuoteBundlePackage_Result[0].EmployeeID" value="" />
<input type="text" name="QuoteBundlePackage_Result[0].EmployeeName" value="" />

<input type="text" name="QuoteBundlePackage_Result[1].EmployeeID" value="" />
<input type="text" name="QuoteBundlePackage_Result[1].EmployeeName" value="" />

... which will allow the framework to distinguish between each item. To create this arrangement you should give each item an ID within your loop - (Sorry in advance for answer being in c# and not vb!)

@for (int i = 0; i < Model.Count(); i++)
{
    @Html.EditorFor(e => e[i].EmployeeID)
    @Html.EditorFor(e => e[i].EmployeeName)
}

See related articles from Scott Hansleman and Phil Haack.

这篇关于MVC格式不张贴日期表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆