【发布时间】:2016-12-28 18:46:03
【问题描述】:
如何在不将其用作输入字段的情况下使用Model 中的变量?我在Model 中有 1 个未使用的变量,即userId。我的JavaScript 下面根据单击的行填充View 中的字段。我还计划填充userId,这样我就可以在我的更新按钮中使用它。如何使用m => m.userId 作为变量?
查看
@Html.LabelFor(m => m.userDesc)
@Html.TextBoxFor(m => m.userDesc)
@Html.LabelFor(m => m.userStatus)
Active @Html.RadioButtonFor(m => m.userStatus, true)
Inactive @Html.RadioButtonFor(m => m.userStatus, false)
型号
public string userId { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "User")]
public string userDesc { get; set; }
[Display(Name = "Status")]
public string userStatus { get; set; }
JavaScript 的一部分
//columnData - represents the column index in my table
//fieldId - id of the fields (e.g. text = userDesc, radio = userStatus)
for (i = 0; i < columnData.length; i++) {
var elmntType = document.getElementById(fieldId[i]).getAttribute("type");
if (elmntType == "text") {
document.getElementById(fieldId[i]).value = rowSelected.cells[i].innerHTML.trim();
} else if (elmntType == "radio") {
var status = rowSelected.cells[i].innerHTML.trim();
if(status == "Active") {
$('input[name="' + fieldId[i] + '"][value="True"]').prop('checked', true);
} else {
$('input[name="' + fieldId[i] + '"][value="False"]').prop('checked', true);
}
}
}
【问题讨论】:
-
刚刚有人对我的问题投了反对票吗!?我认为这是一个有效的。这不是一个重复的问题,我发布了我的代码。天哪。
-
您是否要将
userId的值分配给javascript 变量? - 你可以使用var id = @Html.Raw(Json.Encode(Model.userId));。或者您可以将值添加为 html 元素的data-*属性(并使用var id = $(element).data('....'); -
@PhilipSy:如果您将鼠标悬停在否决按钮上,您会看到描述:“这个问题没有显示任何研究成果;它不清楚或没有用。”事实上,这个问题是 SO 上大约一百个其他问题的副本。只是它的呈现方式有很多变化,它们通常不会像这样关闭。然而,它们都围绕着提问者不理解客户端-服务器的基本概念。换句话说,没有研究工作。我没有对你投反对票,但这对我来说似乎完全有效。
标签: javascript asp.net-mvc parameter-passing strongly-typed-view