从Razor视图提交表单时,模型始终为null

时间:2013-12-02 11:32:39

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

我正在处理ASP.NET MVC 4申请。我有一个名为Currencty.cshtml的视图和我的控制器中的两种方法:

    [HttpGet]
    public ActionResult Currency()
    {
        IEnumerable<Currency> model = unitOfWork.CurrencyRepository.GetAll();
        return View(model);
    }

    [HttpPost]
    public ActionResult Currency(IEnumerable<Currency> model)
    {
        var test = model;

视图本身是:

@model IEnumerable<MyProject.Models.Currency>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<table>
   <thead>
     <tr>
         <th rowspan="2">Code</th>
         <th rowspan="2">Currency</th>
         <th rowspan="2">Fixing</th>
         <th colspan="2">PayDesk</th>
         <th colspan="2">NoDesk</th>
     </tr>
     <tr>
         <th>Buy</th>
         <th>Sell</th>
         <th>Buy</th>
         <th>Sell</th>
     </tr>
   </thead>
   <tbody>
     @foreach (var item in Model)
     { 
        <tr>
            <td>@Html.HiddenFor(modelItem => item.CurrencyID)
                @Html.TextBoxFor(modelItem => item.Code)</td>
            <td>@Html.TextBoxFor(modelItem => item.CurrencyName)</td>
            <td>@Html.TextBoxFor(modelItem => item.FixingRate)</td>
            <td>@Html.TextBoxFor(modelItem => item.PayDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => item.PayDeskSell)</td>
            <td>@Html.TextBoxFor(modelItem => item.NoDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => item.NoDeskSell)</td>
        </tr>
     }
    </tbody>

</table>
    <input type="submit" id="form-submit-button" value="Save" />
}

问题是我在[HttpPost] public ActionResult Currency(IEnumerable<Currency> model)中有一个断点,我可以看到,当我提交表单时,我到达那里,但模型始终为空。一切似乎都很直接我只是看不出有什么问题,所以我无法收回我的数据。当我使用[HttpGet]方法加载表单时,一切正常,我看到了数据。

3 个答案:

答案 0 :(得分:2)

尝试与for循环绑定,而不是foreach

@for(int i=0;i<Model.Count;i++)
    {
        <td>@Html.HiddenFor(modelItem => Model[i].CurrencyID)
                @Html.TextBoxFor(modelItem => Model[i].Code)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
    }

Reference

答案 1 :(得分:1)

尝试在模型上使用for循环和indexers

@for(int i=0;i<Model.Count();i++)
    {
        <td>@Html.HiddenFor(modelItem => item.CurrencyID)
                @Html.TextBoxFor(modelItem => Model[i].Code)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
            <td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
    }

另外,为了应用索引器,您应该将List作为模型传递。所以在你的行动中:

[HttpGet]
public ActionResult Currency()
{
    var model = unitOfWork.CurrencyRepository.GetAll().ToList();
    return View(model);
}

在你看来:

@model List<MyProject.Models.Currency>

如果您想了解有关列表绑定的更多详细信息,我建议您阅读这些文章:

Model Binding To A List by Phill Haacked

ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries by S. Hanselman

答案 2 :(得分:1)

为了能够列出对象列表的所有值并且能够将它们全部提交给POST操作,我遇到了一个奇怪的问题,即我总是将其提交给POST操作。所以,我更改了剃刀视图页面顶部的IEnumerable<>列表 - 此页面为List<>而我没有使用@foreach(),而是使用了@for()带索引

@for (int i = 0; i < Model.Count(); i++)
    {
    <tr>
        <td>@Html.DisplayFor(modelItem => Model[i].UserId)</td>
        @Html.HiddenFor(modelItem => Model[i].UserId)
        <td>@Html.CheckBoxFor(modelItem => Model[i].isLead)</td>
    </tr>
    }