我想用我的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。
我怎样才能使它发挥作用?