ViewModel空列表和post上的空对象



我正在尝试使用视图模型的第一次冒险,但我遇到了一个问题,这是我的视图模型和模型:

ViewModel:

    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接收一个空的专业对象,一个空的列表和另一个零项。

我怎样才能使它工作?

我在你的表格中看到的都是Model.NotAcceptedJobOfferts。如果要维护数据,则需要将其添加为表单中的隐藏输入。一旦视图呈现给用户,您的模型就丢失了,唯一可用的数据就是页面上呈现的数据。

你有很多复杂的类型在发挥作用。默认模型绑定器在绑定要在表单中发布的值时遇到困难。你可以这样做:

  • 通过view source检查HTML中值的名称。确保它们遵循[PropertyNameInParentModelClass]。PropertyNameInChildModelClass公约。例如:专业的。
  • 在action方法中添加断点,并检查表单值。试着看看HttpContext.Current.Request。检查是否所有预期的表单值都如预期的那样存在。
  • 考虑通过去掉你不需要的东西来简化视图模型和模型。当您对系统有一个完整的视图时(希望如此),您应该能够确定。
  • 考虑使用自定义模型绑定。互联网上有很多关于如何定制模型绑定的文献。这里有两个开始:MSDN文章和代码项目示例

相关内容

  • 没有找到相关文章

最新更新