views:

52

answers:

2

Hi, I'm pretty sure that I complicated my question. Sorry, I didn't know how to express myself. Situation is next :

public ActionResult Edit(int id)
    {
        CreateTrainingModel editTrainingModel = new CreateTrainingModel();

        editTrainingModel.Training = training.GetByID(id);
        editTrainingModel.Player = player.GetAll();            

        return View(editTrainingModel);
    }

and Editor template for Html.EditorFor() :

@inherits System.Web.Mvc.WebViewPage<TrainingStatistics.Models.ViewModels.CreateTrainingModel>

@Html.ValidationSummary()

<table>
 @Html.HiddenFor(x => x.Training.TrainingID) 

 <tr>
  <td>Training Date</td>
  <td><input type="text" id="datepicker" name="Training.Date"></td>
  <td>@Html.ValidationMessageFor(x => x.Training.Date)</td>
 </tr> 

</table>

Problem is that when I want to edit this, data from model aren't passed to the view. I think that problem is because of using ViewModels and I'm doing something wrong here..When I try to update something which is consist of only one entity (example Player) and when I'm not using ViewModel everything is fine.

Thank you in advance!

A: 

I believe you need to create editor template for the view model. You can see the exmaples of this below http://www.codecapers.com/post/Display-and-Editor-Templates-in-ASPNET-MVC-2.aspx http://davidhayden.com/blog/dave/archive/2009/08/21/htmleditorforscaffoldcolumnattribute.aspx

Nilesh Gule
+1  A: 

the problem is that you have nested objects inside your viewmodel, and they aren't going to be created. The default constructor is getting called and you will have null values for Training and Player

look here ( http://valueinjecter.codeplex.com/releases/view/50411 ) in the samples solution for a good example of using ViewModels

Omu
They are null in the moment when CreateTrainingModel() is called. But after that I fill Training and Player with data (see next two lines).. But thanks on answer.. I solved a problem .. it was caused by datepicker textbox.. I'm now using @Html.TextBoxFor(x => x.Training.Date, new { id = "datepicker" }) and everything is fine.. problem was because I didn't specify VALUE in input type so controller didn't know who to pass data.. tnx again...
rjovic
@rjovic I meant that it would be null in the post action, e.g. `[HttpPost] public ActionResult Edit(CreateTrainingModel model){//model.Training could be null}`
Omu