View正在发送一个空对象列表

时间:2014-05-08 23:24:17

标签: c# asp.net-mvc asp.net-mvc-4 html-helper

当我尝试从视图发送到控制器时,我遇到了对象的空列表问题, jobOffertModel参数为null

这是我的控制器方法:(我知道它不是什么,但我正在测试参数)

[HttpPost]
public ActionResult AcceptJobOfferts(IEnumerable<JobOffertModel> jobOffertModel)
{
    initBusinessObjects();

    return View();
}

以下是我的观点:

@model IEnumerable<TCCApplication.Models.JobOffertModel>

@{
    ViewBag.Title = "Minhas ofertas de trabalho";
}

<h2>@ViewBag.Title</h2>


@using (Html.BeginForm("AcceptJobOfferts","Professional")){

<div class="row">
    <div class="col-md-9">
        @foreach (var item in Model)
        {
            <div class="row">
                <div class="col-md-2">
                    <label>Oferta: </label>
                    @Html.DisplayFor(itemModel => item.Description)
                </div>

                @if (item.Acepted)
                {
                    <div class="col-md-2">
                        <label>Aceitar?</label>
                        @Html.DisplayFor(itemModel => item.Acepted)
                    </div>
                }
                else
                { 
                    <div class="col-md-2">
                        <label>Aceitar?</label>
                        @Html.EditorFor(itemModel => item.Acepted)
                    </div>
                }
                <div class="col-md-2">
                    <label>Ativa</label>
                    @Html.DisplayFor(itemModel => item.Active)
                </div>
                <div class="col-md-3">
                    <label>Data do trabalho</label>
                    @Html.DisplayFor(itemModel => item.JobDate)
                </div>
            </div>
        }
    </div>

    <div class="col-md-2">
        <input type="submit" value="Aceitar ofertas de trabalho"/>
    </div>

</div>

}

用户使用此方法进行查看:

public ActionResult ViewMyJobOfferts(int professionalId)
{
    initBusinessObjects();

    var professionalJobOfferts = jobOffertBusiness.GetJobOffertsByProfessional(professionalId);

    return View(professionalJobOfferts);
}

2 个答案:

答案 0 :(得分:0)

替换此行:

 @using (Html.BeginForm("AcceptJobOfferts","Professional")){

人:

 @using (Html.BeginForm("AcceptJobOfferts","Professional", FormMethod.Post)){

答案 1 :(得分:0)

改变我所做的事情 -

  1. 使用名为modelList的变量将IEnumerable转换为List()。
  2. 用于循环以显示所有属性值。
  3. 使用隐藏字段仅保留POST操作的显示元素属性。
  4. 将POST控制器操作的参数名称更改为modelList。
  5. 你必须得到这样的观点 -

    @model IEnumerable<TCCApplication.Models.JobOffertModel>
    
    @{
        ViewBag.Title = "Minhas ofertas de trabalho";
    }
    
    <h2>@ViewBag.Title</h2>
    
    
    @using (Html.BeginForm("AcceptJobOfferts","Professional", FormMethod.Post)){
    var modelList = Model.ToList();
    <div class="row">
        <div class="col-md-9">
            for (int i = 0; i < modelList.Count; i++)
            {
                <div class="row">
                    <div class="col-md-2">
                        <label>Oferta: </label>
                        @Html.DisplayFor(itemModel => modelList[i].Description)
                        @Html.HiddenFor(itemModel => modelList[i].Description)
                    </div>
    
                    @if (modelList[i].Acepted)
                    {
                        <div class="col-md-2">
                            <label>Aceitar?</label>
                            @Html.DisplayFor(itemModel =>  modelList[i].Acepted)
                            @Html.HiddenFor(itemModel =>  modelList[i].Acepted)
                        </div>
                    }
                    else
                    { 
                        <div class="col-md-2">
                            <label>Aceitar?</label>
                            @Html.EditorFor(itemModel =>  modelList[i].Acepted)
                        </div>
                    }
                    <div class="col-md-2">
                        <label>Ativa</label>
                        @Html.DisplayFor(itemModel => modelList[i].Active)
                        @Html.HiddenFor(itemModel => modelList[i].Active)
                    </div>
                    <div class="col-md-3">
                        <label>Data do trabalho</label>
                        @Html.DisplayFor(itemModel =>  modelList[i].JobDate)
                        @Html.HiddenFor(itemModel =>  modelList[i].JobDate)
                    </div>
                </div>
            }
        </div>
    
        <div class="col-md-2">
            <input type="submit" value="Aceitar ofertas de trabalho"/>
        </div>
    
    </div>
    
    }
    

    然后你的控制器应该是 -

    [HttpPost]
    public ActionResult AcceptJobOfferts(IEnumerable<JobOffertModel> modelList)
    {
        initBusinessObjects();
    
        return View();
    }