我尝试使用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接收一个空的Professional对象,一个空List和另一个零itens。
我怎样才能让它发挥作用?
答案 0 :(得分:1)
我在表单中看到的只有Model.NotAcceptedJobOfferts
。如果要维护数据,则需要将其作为表单中的隐藏输入添加。一旦视图呈现给用户,您的模型就会丢失,唯一可用的数据就是在页面上呈现的内容。
答案 1 :(得分:0)
你有很多复杂的类型。默认模型绑定器难以绑定在表单中发布的值。这是你可以做的事情: