模型没有从视图传递到控制器

时间:2013-03-28 09:37:00

标签: javascript asp.net-mvc-3

我是mvc和javascript的新手。首先我使用javascript来修复divsion中的parital视图

$('.btngo').click(function (e) {
        var fid = $('#FiscalYear_FYId').val();
      alert($('#FiscalYear_FYId').val());
        $.ajax({
            type: 'Get',
            url: '@Url.Action("RateList", "Rate")',
            data: { fyid: fid },
            success: function (sc) {
                $('#Ratelist').html(sc);

            }
        });
    });

部分视图是模型FHIControl.Model.StationeryRate.RateDTO,它包含一个提交按钮,我的视图看起来像

@using (Html.BeginForm("Ratelist", "Rate", FormMethod.Post))
{
    @Html.ValidationSummary(true)

    <table>
    <thead>
    <tr>
    <th>Item Id</th>
    <th>Item Name</th>
    <th>Rate</th>

    </tr>
    </thead>

   @Html.HiddenFor(x=>Model.FiscalYear.FYId)
     @foreach (var item in Model.RateList)
      {
        <tr>
             @Html.HiddenFor(x => item.ItemId)
         <td>@{count++;}@count</td>
        <td>@Html.DisplayFor(x => item.ItemName)</td>
        <td>@Html.TextBoxFor(x => item.Rate)</td>
        </tr>
      }


</table>
        <p>
            <input type="submit" value="Ok" id="btnsubmit" />
        </p>
}

按钮提交是提交表单但没有模型项目。为什么会这样?有什么方法可以使这个工作吗?

1 个答案:

答案 0 :(得分:0)

没有模型项,因为您只传递了FiscalYear_FYId

的值
    var fid = $('#FiscalYear_FYId').val();
    $.ajax({
        data: { fyid: fid },                    
    });

应该是:

    $.ajax({
        data: $form.serialize(),            
    });

其中$form是对表单的引用。您可以为更快更好的参考命名,也可以像这样引用它:

    var $form = $("#btnsubmit").parents('form');