html.hidden在更改name属性时丢失值



我在my.cshtml视图中有一个@html.editorfor@html.hiddenfor元素,在按钮单击事件中,我更改了这两个元素的名称属性,以便将它们发布在单独的列表中。

editorfor元素的值应该是模型中的字符串,但hiddenfor元素的值则应该是int

问题是,新列表返回的是正确的编辑器值,但隐藏值总是返回"0",即使它应该是其他值。有没有其他方法可以将int值重新发布到列表中?

顺便说一句,当我在不更改元素名称属性的情况下发布元素的值时,一切都是正确的。

下面是我用来更改名称属性的脚本:

//inside for loop where i is the iteration index
@Html.HiddenFor(m => m[i].EmployeeTimeSeriesDataID, new { htmlAttributes = new { @id = "tsdata_hidden_" + i } })
<script>
 $('somebutton').click(function() {
  var hiddenTsID = "#tsdata_hidden_" + aNumber; //number to match the i from the hidden for
  var nameOfHiddenToDeleteWithIndex = "deleteList[" + deleteIndex + "].EmployeeTimeSeriesDataID";
  //deleteindex starts at 0 and is incremented at the end of every button click, so the new list
  //can be correctly created starting with index[0]
  $(hiddenTsID).attr('name', nameOfHiddenToDeleteWithIndex);
 });
</script>

控制器动作:

public ActionResult TimeSeriesData(/*among other params*/EmployeeTimeSeriesData[] deleteList) {
 //working update functionality
 //working add functionality
 foreach (EmployeeTimeSeriesData tsData in deleteList)
  {
   EmployeeTimeSeriesData y = db.EmployeeTimeSeriesData.Find(tsData.EmployeeTimeSeriesDataID); 
   db.EmployeeTimeSeriesData.Remove(y);
  }
  db.SaveChanges();
}

显然html hiddenfor助手有问题。当我试图检索值时,它返回的是空白值,例如:

confirm($('tsdata_hidden_0').attr('value'))

这打开了一个带有空消息的确认框,'value'没有出现。当我尝试相同的东西但具有不同的属性时,也发生了相同的事情(我尝试了'name''id',而不是'value',它们都是空白的。)

然后我做了一个常规的html隐藏输入

<input type="hidden" id="someID" name="someName" value="someValue"/>

现在当我做的时候

confirm($('someID').attr('value'))

一个确认框显示"someValue"作为消息,不再为空。再次感谢你,微软,让的生活变得艰难

您的列表活页夹可能有问题,请尝试:

var nameOfHiddenToDeleteWithIndex =
    "deleteList[" + deleteIndex + "].EmployeeTimeSeriesData.EmployeeTimeSeriesDataID";

相关内容

最新更新