我在更新MVC应用程序中的对象时遇到问题:
这是我的整个视图,它有一个提交按钮,必须在NotAcceptedJobOfferts
列表中更新它:
@model TCCApplication.ViewModels.ProfessionalDashboard
@{
ViewBag.Title = "Dashboard";
}
@using (Html.BeginForm("AcceptJobOfferts","Professional")){
<div class="row">
<div class="col-md-5">
@Html.HiddenFor(model => model.Professional.Id)
<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">
<h2>Ofertas de trabalho indicadas</h2>
@for (int i = 0; i < Model.NotAcceptedJobOfferts.Count; i++)
{
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Id)
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Description)
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Active)
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Accepted)
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].JobDate)
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Professional.Id)
<div class="row">
<div class="col-md-3">
<label>Oferta: </label>
@Html.EditorFor(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" />
</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>
}
但是当我将数据发送到控制器时,对象没有更新:
我的控制器方法:
public ActionResult ViewMyTasks(int professionalId)
{
initBusinessObjects();
ProfessionalDashboard dashboard = new ProfessionalDashboard();
var notAcceptedJobOfferts = jobOffertBusiness.GetNotAcceptedJobOfferts(professionalId).ToList();
var acceptedJobOfferts = jobOffertBusiness.GetAcceptedJobOfferts(professionalId).ToList();
var professional = professionalBusiness.GetById(professionalId);
dashboard.AcceptedJobOfferts = acceptedJobOfferts;
dashboard.NotAcceptedJobOfferts = notAcceptedJobOfferts;
dashboard.Professional = professional;
return View(dashboard);
}
[HttpPost]
public ActionResult AcceptJobOfferts(ProfessionalDashboard profDash)
{
initBusinessObjects();
foreach (var jo in profDash.NotAcceptedJobOfferts)
{
jobOffertBusiness.Update(jo);
}
return RedirectToAction("ViewMyTasks", new { professionalId = profDash.Professional.Id});
}
我必须对我的观点发送更新的数据?
答案 0 :(得分:0)
试试这个
@using(Html.BeginForm("Action","Controller",FormMethod.Post))
{
...
...
}
答案 1 :(得分:0)
隐藏字段与输入具有相同的名称,这会导致重复的表单键。您需要删除隐藏或更改其名称。
@Html.HiddenFor(itemModel => Model.NotAcceptedJobOfferts[i].Description)
// eg. <input type="hidden" name="NotAcceptedJobOfferts[0].Description" />
...
@Html.EditorFor(itemModel => Model.NotAcceptedJobOfferts[i].Description)
// eg. <input type="text" name="NotAcceptedJobOfferts[0].Description" />