Html Helper 可能缓存输入值

发布于 2024-12-11 03:58:31 字数 2120 浏览 0 评论 0原文

我正在使用视图模型和部分视图来使用 AJAX 插入行。插入新行时,ViewModel 的 ID (AttributeDefinitionID) 将设置为零。保存时,ID 会更新并发送回视图。然而,绑定到 ID 的助手似乎仍然具有旧值。

视图模型

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

部分视图“_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>

由于某种原因,Html Helper Html.HiddenFor 未绑定到正确的值并生成以下 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">

您可以看到它已正确插入 ID在行标记 () 中,但不在输入标记 (value="0") 中。应该是 value="40850"

有什么想法吗? Html Helper 或浏览器缓存该值吗?

编辑: AJAX 函数 AddAttribute 只是调用同名的控制器函数,并将结果部分(上面列出的部分)附加到表中:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错々过的事 2024-12-18 03:58:31

差异的原因与浏览器中的缓存无关。浏览器会将您的 HTML 页面作为一个整体进行缓存,而不是其中的一部分(更正确的是:浏览器不会缓存 HTTP 请求结果的一部分)

您的问题是 POST 请求中的值与 POST 请求中的值相比存在差异模型。这篇帖子对此进行了深入解释

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文