2017-05-05 10 views
0

私はインデックスと詳細ビューを持つMVCアプリケーションを持っています。索引を表示する_peticionesリスト項目。そして、このリストを詳細ビューから更新したいと思います。他のクラスのリストを更新する

詳細ビュー

@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> 
    } 

コントローラ(Updateメソッドは、パラメータとして "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

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; 
     } 
} 

はまた、理想的な世界にあなたのサービスをコントローラに注入しなければならない更新を処理するためのコントローラを作成インスタンス化。しかし、それは別の問題です。

上記が役に立ちますようお願いいたします。

関連する問題