2017-05-05 86 views
0

我有一個帶有索引和詳細視圖的MVC應用程序。索引顯示_peticiones列表項。我想從Detail視圖更新這個列表。其他職位的更新列表

詳情查看

@using (Html.BeginForm()) 
    { 
     <div class="form-group"> 
      <label for="id">Id:</label> 
      @Html.TextBoxFor(m => m.Id, new { @id = "id", @class = "form-control", @readonly = "readonly" }) 
     </div> 
     <div class="form-group"> 
      <label for="nombre">Nombre:</label> 
      @Html.TextBoxFor(m => m.Nombre, new { @nombre = "nombre", @class = "form-control"}) 
     </div> 
     <div class="form-group"> 
      <label for="desc">Descripcion:</label> 
      @Html.TextBoxFor(m => m.Desc, new { @dec = "desc", @class = "form-control"}) 
     </div> 
     <div class="form-group"> 
      <label for="fecha">Fecha:</label> 
      @Html.TextBoxFor(m => m.Fecha, new { @fecha = "fecha", @class = "form-control"}) 
     </div> 
     <div class="form-group"> 
      <label for="activo">Activo:</label> 
      @Html.TextBoxFor(m => m.Activo, new { @activo= "activo", @class = "form-control" }) 
     </div> 
     <div class="container-fluid"> 
      <div class="col-md-12 text-right"> 
       <input type="submit" value="Guardar" class="btn btn-primary" /> 
      </div> 
     </div> 
    } 

控制器(更新方法具有 「ID」 爲參數,我無法使用對象像的參數)

public ActionResult Detail(Peticion peticion) 
    { 
     if (ModelState.IsValid) 
     { 
      var id = peticion.Id; 
      _peticionService.Update(id); 
      return RedirectToAction("Index"); 
     } 
     return View(); 
    } 

類PeticionService

public bool Update(int id) 
    { 
     if (id > 0) 
     { 
      var peticionOld = _peticiones.FirstOrDefault(u => u.Id == id); 
      if (peticionOld != null) 
      { 
       //How to update item list?? 
       return true; 
      }     
     } 
     return false; 
    } 

如何更新列表從「PeticionService」類只有ID?

+0

_peticiones來自Update語句?你想在列表中更新什麼?我假設的Peticion領域? – Wheels73

+0

_peticiones是在PeticionesService類中創建的列表。這是我試圖更新的列表。 – Gelabert

+0

對不起,我的意思是在Update語句的上下文中。 MVC是無狀態的。當您發佈到「更新」時,_peticiones如何設置? – Wheels73

回答

0

我會接近你的問題,如下所示。

爲您的視圖創建視圖模型。

public class PeticoneViewModel 
{ 
    public Peticione CurrentPeticione { get; set; } 
} 

然後有一個處理列表源的檢索和更新的服務。

public class PeticoneService 
{ 
     public Peticione GetPeticioneByID(int id) 
     { 
      //Put your implementation here. 
      return new Peticione(); 
     } 

     public bool SavePeticioneByID(int id, string newValue) 
     { 
      //Put your implementation here. 
      return true; 
     } 
} 

然後創建一個控制器返回詳細地看待和處理更新

public class PeticioneController : Controller 
{ 
     public ActionResult Detail(int peticonID) 
     { 
      var peticoneService = new PeticoneService(); 

      var peticoneViewModel = new PeticoneViewModel(); 
      peticoneViewModel.CurrentPeticione = peticoneService.GetPeticioneByID(peticonID); 

      return View("Detail",peticoneViewModel); 
     } 

     public bool UpdatePeticone(int id, string newValue) 
     { 
      if (id > 0) 
      { 
       var peticoneService = new PeticoneService(); 
       return peticoneService.SavePeticioneByID(id, newValue); 
      } 

      return false; 
     } 
} 

此外,在一個理想的世界,你的服務應該被注入控制器,該控制器是不負責的服務實例。但那是另一個問題。

希望以上幫助。