2014-05-11 65 views
0

在我的MVC應用程序更新對象,當我一個問題就是:提交按鈕不改變後

這是我的整個觀點,它有一個提交按鈕,必須更新內部NotAcceptedJobOfferts列表itens:

@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

這是http://stackoverflow.com/questions/23598213/viewmodel-empty-list-and-null-object-on-post/23599153#23599153 – Yogiraj

+0

@Yogiraj的副本,沒有它不是重複,我在問爲什麼我的發佈對象爲空,以及爲什麼我的視圖不發佈其更改。 – guisantogui

回答

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" /> 
+0

你剛剛救了我的命!我認爲我已經測試過了,而且沒有奏效,但看起來我做了一個非常糟糕的測試。 – guisantogui

0

試試這個

@using(Html.BeginForm("Action","Controller",FormMethod.Post)) 
{ 
    ... 
    ... 
}