Reputation: 225
How can i get the value of checkbox value of showHideDeletedRps
in a variable? I have the following code in my view I used dependency injection to populate fields in a view but i like to get the value of checkbox to pass it with GetRPByID()
method. Is it possible to accomplish that in asp.net core?
Here is my code in razor view
@{
var showDeleted = get_showDeletedRpValue // I like to get the checkbox value here
var id = Model.ID;
var rpInfo = await rpService.GetRPByID(id, showDeleted);
}
<div class="card">
<input name="showHideDeletedRps" id="showHideDeletedRps" type="checkbox" value="true">
</div>
Code added
@{
var showDeleted = true; // or false, depends on what you want to be default
if (Context.Request.Form.TryGetValue("showHideDeletedRps", out var formValue) &&
bool.TryParse(formValue, out var value))
{
showDeleted = value;
}
}
Upvotes: 1
Views: 1096
Reputation: 11154
Code that supports either form data or query string
@{
var showDeleted = true; // or false, depends on what you want to be default
if (Context.Request.HasFormContentType && Context.Request.Form.TryGetValue("showHideDeletedRps", out var formValue)
&& bool.TryParse(formValue, out var parsedFormValue))
{
showDeleted = parsedFormValue;
}
else if (Context.Request.Query.TryGetValue("showHideDeletedRps", out var queryValue)
&& bool.TryParse(queryValue, out var parsedQueryValue))
{
showDeleted = parsedQueryValue;
}
}
For autopostback, put this script at the bottom of your page (make sure jQuery is included before this script). Works with checkboxes, radiobuttons and selects.
<script type="text/javascript">
$("select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]").on("change", function () {
$(this).closest("form").submit();
});
</script>
and change checkbox like this
<input autopostback="true" name="showHideDeletedRps" id="showHideDeletedRps" type="checkbox" value="true">
Upvotes: 1