视图正在发送对象的空列表

本文关键字:列表 对象 视图 | 更新日期: 2023-09-27 18:02:33

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

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

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

And Here is my 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);
}

视图正在发送对象的空列表

改变我所做的-

  1. 将IEnumerable转换为List(),变量名为modelList。
  2. 用于显示所有属性值。
  3. 使用隐藏字段来保留POST操作时只显示元素属性。
  4. POST Controller Action的参数名改为modelelist

你的视图应该是这样的-

@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();
}

替换这一行:

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

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