title: Razor view engine and model in html helpers
body: <p>I'm trying to create form from such model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class NewContractorModel
  {
      //...
      public PhotoModel photos { get; set; }
      //...
  }
  class PhotoModel
  {
      public List&lt;Photo&gt; f { get; set; }
  }
  </code></pre>
  
  <p>From controller I do some manipulation (actually I removed some photos from the collection) on the model object and put them into the view page using this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>return new View("SomeView", model);
  </code></pre>
  
  <p>I've tried to create inputs (lets say hidden inputs) for each Photo.</p>
  
  <pre class="lang-cs prettyprint-override"><code>for (int i = 0; i &lt; Model.photos.f.Count; ++i)
  {
      @Html.HiddenFor(m =&gt; m.photos.f[i].Uri)
      @Html.HiddenFor(m =&gt; m.photos.f[i].ThumbnailUri)
      @Html.HiddenFor(m =&gt; m.photos.f[i].SmallThumbnailUri)
      @Html.TextBoxFor(m =&gt; m.photos.f[i].Description, new { placeholder = "Dodaj opis" })
  }
  </code></pre>
  
  <p>But as I noticed that this doesnt work because it dismiss all of model modifications (it still stores all Photos in List despite the fact that I've removed them in Controler method).</p>
  
  <p>Then I tried this code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>for (int i = 0; i &lt; Model.photos.f.Count; ++i)
  {
      Photo photo = Model.photos.f[i];
      &lt;input id="photos_f_@{@i}__Uri" name="photos.f[@{@i}].Uri" type="hidden" value="@photo.Uri"/&gt;
      &lt;input id="photos_f_@{@i}__ThumbnailUri" name="photos.f[@{@i}].ThumbnailUri" type="hidden" value="@photo.ThumbnailUri"/&gt;
      &lt;input id="photos_f_@{@i}__SmallThumbnailUri" name="photos.f[@{@i}].SmallThumbnailUri" type="hidden" value="@photo.SmallThumbnailUri"/&gt;
      &lt;input id="photos_f_@{@i}__Description" name="photos.f[@{@i}].Description" placeholder="Dodaj opis" type="text" value="@photo.Description"/&gt;
  }
  </code></pre>
  
  <p>...and this time IT WORKS!</p>
  
  <p>Can anyone explain me what is the difference between those two parts of code?
  I've tried to swich this code more than ten times and it always work the same so it's not my fault. ;)
  I think that there is a bug in HtmlHelper methods but is there any walk-around ? I'd like to use helpers methods instead of raw html.</p>
  
  <p>EDIT: 
  This is simplified controller class.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyController
  {
      private NewContractorModel _model = null;
  
      protected override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          _model = SerializationUtility.Deserialize(Request.Form["Data"]) as NewContractorModel;
          if (_model == null)
                   _model = TempData["Data"]  as NewContractorModel;
          if (_model == null)
                   _model = new NewContractorModel() as NewContractorModel;
  
          TryUpdateModel(_model);
      }
  
      protected override void OnResultExecuted(ResultExecutedContext filterContext)
      {
          if (filterContext.Result is RedirectToRouteResult)
              TempData["Data"] = _model;
      }
  
      private bool CheckModel(object model)
      {
          Type type = model.GetType();
  
          PropertyInfo[] properties = type.GetProperties();
          foreach (PropertyInfo p in properties)
          {
              object[] attr = p.GetCustomAttributes(true);
              foreach (object a in attr)
              {
                  if (a is ValidationAttribute)
                  {
                      object value = p.GetValue(model, null);
                      if (!((ValidationAttribute)a).IsValid(value))
                          return false;
                  }
              }
          }
          return true;
      }
  
      protected ActionResult SelectPage(string delPhoto)
      {
          if (!CheckModel(_model))
          {
              // Do some action
          }
          //.....
          foreach (ZAY.Database.Photo p in _model.photos.f)
          {
              if (p.Uri == Request["delPhoto"])
              {
                  _model.photos.f.Remove(p);
                  break;
              }
          }
          //.....
          return View("SomeView", _model);
      }
  }
  </code></pre>
  
  <p>I noticed that inside lambdas the model looks just like after TryUpdateModel call (before modifications). If I don't use lambdas the model is modified... :/</p>
  
  <p>And also my Photo class (generated from EntityFramework - so there is nothing interesting) and also simplified:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Photo : EntityObject
  {
      [Required]
      public string Uri { get; set; }
  
      [Required]
      public string ThumbnailUri { get; set; }
  
      [Required]
      public string SmallThumbnailUri { get; set; }
  
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>I'm sorry that I'm writing only such small snippets but the whole code is more complicated - there is only the most interesting part of it.</p>