视图模型空列表和后空对象(ViewModel Empty list and null object

2019-10-20 03:25发布

我想用我的ViewModels第一次冒险,但我有一个问题,这是我的视图模型和模型:

视图模型:

    public class ProfessionalDashboard
    {
        public List<JobOffertModel> AcceptedJobOfferts { get; set; }
        public List<JobOffertModel> NotAcceptedJobOfferts { get; set; }

        public ProfessionalModel Professional { get; set; }
    }

楷模:

    public class JobOffertModel
    {
        [Key]
        public int Id { get; set; }

        public ProfessionalModel Professional { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        [DefaultValue(false)]
        public bool Accepted { get; set; }

        [Required]
        [DefaultValue(true)]
        public bool Active { get; set; }

        public ICollection<SkillModel> Skills { get; set; }

        [Required]
        [Column(TypeName = "DateTime2")]
        public DateTime JobDate { get; set; }

        public virtual CustomerModel Customer { get; set; }

    }

而...

public class ProfessionalModel
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [Column(TypeName = "DateTime2")]
    public DateTime BirthDate { get; set; }
    public int PhoneNumber { get; set; }
    public string Profession { get; set; }

    public UserAddressModel UserAddress { get; set; }

    [Required]
    public UserAccountModel UserAccount { get; set; }

    public ICollection<SkillModel> Skills { get; set; }

    public ProfessionalModel() {}
}

这是我的看法:

@model  TCCApplication.ViewModels.ProfessionalDashboard

@{
    ViewBag.Title = "Dashboard";
}

<div class="row">
    <div class="col-md-5">

        <h2>Minhas tarefas</h2>

        @for (int i = 0; i < Model.AcceptedJobOfferts.Count; i++)
        {

            @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Id)

            <div class="row">
                <div class="col-md-3">
                    <label>Oferta: </label>
                    @Html.DisplayFor(itemModel => Model.AcceptedJobOfferts[i].Description)
                </div>

                @*<div class="col-md-2">
                    <label>Aceito</label>
                    @Html.DisplayFor(itemModel => Model.AcceptedJobOfferts[i].Accepted)
                </div>*@

                <div class="col-md-5">
                    <label>Data do trabalho</label>
                    @Html.DisplayFor(itemModel => Model.AcceptedJobOfferts[i].JobDate)
                </div>


                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Professional.Id)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Professional.Name)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Professional.Skills)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Professional.BirthDate)

                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].JobDate)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Active)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Description)
                @Html.HiddenFor(itemModel => Model.AcceptedJobOfferts[i].Accepted)

            </div>
        }
    </div>
    <div class="col-md-7">
        @using (Html.BeginForm("AcceptJobOfferts","Professional")){

            for (int i = 0; i < Model.NotAcceptedJobOfferts.Count; i++)
            {
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional.Id)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional.Name)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional.Skills)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional.BirthDate)

                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].JobDate)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Active)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Description)
                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Accepted)        

                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts)

                <h2>Ofertas de trabalho indicadas</h2>

                @Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Id)

                <div class="row">
                    <div class="col-md-3">
                        <label>Oferta: </label>
                        @Html.DisplayFor(itemModel => Model.NotAcceptedJobOfferts[i].Description)
                    </div>

                    <div class="col-md-2">
                        <label>Aceito</label>
                        @Html.EditorFor(itemModel => Model.NotAcceptedJobOfferts[i].Accepted)
                    </div>

                    <div class="col-md-5">
                        <label>Data do trabalho</label>
                        @Html.DisplayFor(itemModel => Model.NotAcceptedJobOfferts[i].JobDate)
                    </div>  

                </div>
            }
            <div>
                <input type="submit" value="Aceitar Ofertas" /> <!-- This is the post button -->
            </div>

        }
    </div>

</div>


<div class="row" style="height:50px">




</div>



<div class="row">

    <div class="col-md-12 text-center">
        <div>
            <label>Cliente: </label>

            @Model.Professional.Name
        </div>

        <div>
            <label>Data de nascimento: </label>

            @Model.Professional.BirthDate
        </div>

        <div>
            <label>Telefone: </label>

            @Model.Professional.PhoneNumber
        </div>

        <div>
            <label>Usuário: </label>

            @Model.Professional.UserAccount.Username
        </div>

        <div>
            <label>Profissão: </label>

            @Model.Professional.Profession
        </div>

        <div>
            <label>Bairro: </label>

            @Model.Professional.UserAddress.Neighborhood
        </div>

        <p class="div-to-btn">
            @Html.ActionLink("Alterar informações", "Edit", new { id = @Model.Professional.Id })
        </p>

    </div>
</div>

这是谁必须接受POST方法的方法:

[HttpPost]
public ActionResult AcceptJobOfferts(ProfessionalDashboard profDash)
{
    initBusinessObjects();

    //foreach (var jo in jobOffertModel)
    //{
    //    jobOffertBusiness.Update(jo);
    //}

    return RedirectToAction("ViewMyTasks", new { professionalId = profDash.Professional.Id});
}

我的参数profDash收到null专业对象,空列表和其他零个itens。

我怎样才能使它发挥作用?

Answer 1:

所有我在你的表格看到的是Model.NotAcceptedJobOfferts 。 如果你想保持你的数据,你需要将其添加为您的形式隐藏输入。 一旦视图呈现给用户,你的模型丢失,唯一可用的数据是什么已经在网页上呈现。



Answer 2:

你必须发挥足够复杂的类型。 默认的模型粘合剂具有约束力的越来越张贴在表单中的值的困难。 这里的东西,你可以这样做:

  • 这样做查看源检查HTML中值的名称。 。确保他们遵循[PropertyNameInParentModelClass] [PropertyNameInChildModelClass] convention.For例如:Professional.Name
  • 在操作方法添加一个破发点,并检查形式values.Try看着HttpContext.Current.Request。 检查所有预期表单值是否有预期。
  • 考虑切割出你不会需要的东西简化视图模型和模型位。 当你有你的系统(希望)的完整视图,你应该能够确定。
  • 请考虑使用自定义模型粘合剂。 有大量的文献互联网上有关如何使自定义模型粘合剂。 这里有一对夫妇开始: MSDN文章和代码示例项目


文章来源: ViewModel Empty list and null object on post