2

I am using a view model and a partial view to insert a row using AJAX. When a new row is inserted the ViewModel's ID (AttributeDefinitionID) is set to zero. On save, the ID is updated and sent back out the view. However the helper that binds to ID seems to still have the old value.

View model

public class AttributeEntryViewModel
{
    public int AttributeDefinitionID { get; set; }

    [Required]
    [MaxLength(255, ErrorMessage = "Name must be less than 
                                    255 characters in length.")]
    public string Name { get; set; }
}

Partial view "_AttributeEntryPartial.cshtml"

@model ICMDB.ViewModels.AttributeEntryViewModel

<tr id ="@Model.AttributeDefinitionID" >
   @Html.HiddenFor(model => model.AttributeDefinitionID)

   <td>
      @Html.EditorFor(model => model.Name)
      @Html.ValidationMessageFor(model => model.Name)
   </td>
   <td>
      <a href="#" onclick="RemoveAttribute(@Model.AttributeDefinitionID); 
                           return false;">Remove</a>
   </td>
</tr>

For some reason, the Html Helper Html.HiddenFor doesn't bind to the correct value and produces the following Html:

<tr id="40850">
    <input id="AttributeDefinitionID" type="hidden" value="0" 
           name="AttributeDefinitionID" data-val-required="The 
           AttributeDefinitionID field is required." data-val-number= "The 
           field AttributeDefinitionID must be a number." data-val="true">

You can see that it has inserted the ID correctly in the row tag (<tr id = "40850">) but not in the input tag (value="0"). That should read value="40850".

Any ideas? Is the Html Helper or the browser caching the value?

EDIT: The AJAX function AddAttribute simply calls a controller function of the same name and appends the resulting partial (the partial listed above) to a table:

function AddAttribute() {
   // and send it as AJAX request
   $.ajax({
      url: '@Url.Action("AddAttribute")',
      type: 'POST',
      cache: false,
      success: function (result) {
         // when the AJAX succeeds add result to the table
         $('#AttributesTable').append(result);
      }
   })
}

[HttpPost]
public ActionResult AddAttribute()
{
   var model = new AttributeEntryViewModel();
   return PartialView("_AttributeEntryPartial", model);
}
dnatoli
  • 6,972
  • 9
  • 57
  • 96
  • Can you explain the AJAX part in depth? I've no idea if this issue is somehow AJAX related because I'm not seeing any AJAX code :) – saintedlama Oct 21 '11 at 05:10

1 Answers1

2

The reason for the differences has nothing to do with caching in the browser. The browser would cache your HTML page as a whole an not parts of that (more correct: The browser will not cache parts of the result of an HTTP request)

Your problem is the difference of the value in the POST request compared to the value in the model. This is explained in depth in this post

Community
  • 1
  • 1
saintedlama
  • 6,838
  • 1
  • 28
  • 46
  • 1
    You are right, which actually exposes a bigger stuff up on my part - I wasn't redirecting after the POST. There was a link in the comments of the post you linked to that summed it up pretty well: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx. – dnatoli Oct 21 '11 at 05:41