How can I use the variable in Model without using it as an input field? I have 1 unused variable in Model which is the userId. My JavaScript below populates the fields in the View depending on the clicked row. I'm planning to populate also the userId so I can use it in my Update button. How do I use m => m.userId as a variable?
View
@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)
Model
public string userId { get; set; }
[Required(ErrorMessage = "*")]
[Display(Name = "User")]
public string userDesc { get; set; }
[Display(Name = "Status")]
public string userStatus { get; set; }
part of 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);
}
}
}
First of all, you can use variables contained in
Modelwithout using them => m.Anotation. Anywhere in your view, you can do something like that :<h1>Hi @Model.userDesc</h1>Now, if you want to use your
Model's variables in JS code, you can achieve with 3 ways :@Model.userIdanywhere you want.@Model.userIdPlus, if you want to update the
userIdvariable from JS code, you have to store it in a hidden field in order to manipulate it.