0

my problem is returning Model back to controller... I have got a cooking recipes website. and two Models

public class FeedBackListViewModel
{      
    public int  FeedBackID { get; set; }
    public string FoodName{ get; set; }
    public List<StepModel> Steps { get; set; }
}

public class Food 
{    
     public int ID {get;set;}
     public string FoodName { get; set; } 
     public string Time { get; set; }
     public List<StepModel> Steps { get; set; }
 }

public class StepModel 
{    
     public int StepID {get;set;}
     public int StepNo { get; set; } 
     public string StepDetail { get; set; }
     public bool Achived { get; set; }
}

public IActionResult Edit(int id)
{
   // Imagine i have got a Food[0].Steps{Step1, Step2, Step 3}
       each food has different number of Steps.
    return View(Food[0]);
}

cshmtl file... I simplified the code for easy read

 @model FeedBackListViewModel

 <form asp-action="Edit">
    <input asp-for="FoodName " class="form-control" />
    @foreach (var item in Model.Steps)
          {
       <tr>
           <td>@Html.DisplayFor(model => item.StepID)</td>
           <td>@item.StepNo</td>
            <td>
            <input type="text" id="@item" value="@item.StepDetail" />
                    </td>
                    <td><select asp-for="@item.Achivmed">
                           <option value = yes> Yes </option>
                           <option value = No> No </option>
                        </select> 
                    </td>                        
        </tr   >
            }



[HttpPost]
public IActionResult Edit(FeedBackListViewModel viewModel)
{
    // I got my foodName detail correct but Steps are always null. 
    // I can't retrieve changes on steps. 

    return View();
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
AliAzra
  • 889
  • 1
  • 9
  • 28

2 Answers2

-1

Try below. I think the Steps is not being initialized when the FeedBackListViewModel get initialize. public class FeedBackListViewModel {
public int FeedBackID { get; set; } public string FoodName{ get; set; } public List Steps { get; set; }

   public FeedBackListViewModel()
   {
      Steps = new List<StepModel>(); 
   }
}
RSF
  • 510
  • 6
  • 18
  • Where am i going to inidialise it? Because Html Edit form is correct. But when i submit it back to controller , STEP is null. – AliAzra Jan 16 '19 at 22:20
  • HI, ViewModel could be initialized, it doesn't mean your list get initialized as it's a separate object, IMO, you have to explicitly initialize the StepModel list. – RSF Jan 16 '19 at 22:22
  • Hi again. OK I initialised it in view constructor as you said. Return Step is empty now... Some how cshtml doesn't fill Step model from form. – AliAzra Jan 16 '19 at 22:35
  • @AliAzra, you are making a progress. Now you can look into why the form does not populate the list. Cheers! – RSF Jan 16 '19 at 22:38
  • @AliAzra, perhaps you need to fill your steps from the server and let view engine to render it. I see you have a for-ech in the view. That means the view expecting a filled steps list to be looped – RSF Jan 16 '19 at 22:40
  • The question is passing the Model from the View to the Controller. You got it backwards making this a useless answer. – Camilo Terevinto Jan 16 '19 at 22:44
  • @CamiloTerevinto, look carefully, he is trying a loop in the view. How could it possible to loop through an empty list if the list is empty? we are trying to help him to get the view work and then help him to read the values been passed through the view. – RSF Jan 16 '19 at 22:47
  • You can take a look at the question's title "Pass complex Model from View to Controller". The comment that the list is empty is in the POST not in the GET – Camilo Terevinto Jan 16 '19 at 22:49
  • @CamiloTerevinto, Agree. an oversight. – RSF Jan 16 '19 at 23:05
  • Thanks guys for help.. I fixed it with for loop instead of foreach. I am not sure the difference but it is working now. – AliAzra Jan 16 '19 at 23:34
-1

First and foremost, I don't see the code where you injected data into the collection.

public class FeedBackListViewModel
{      
    public int FeedBackID { get; set; }
    public string FoodName{ get; set; }
    public List<StepModel> Steps { get; set; } = new List<StepModel>();

}

Also, there's a typo here

<select asp-for="@item.Achivmed">

Try again!

Nicholas
  • 1,883
  • 21
  • 39