'Razor page on edit passing null value
I am learning razor pages. I want to be able to update the current record in a sql table on edit of a razor page. However, My model value is always null on post and I'm not sure what the value needs to be to be able to save it to the db. I have tried several things but none are working. I can hard code it to a value and it updates the db. But GroupName and the other values are always null. Any help is appreciated. I've looked at a lot of tutorials, but nothing seems to be helping. Thanks in advance.
My cshml page:
<form method="post">
@Html.ValidationSummary(true)
@*<input asp-for="tblAllTransactionsWithDetails.transID" type="hidden" />*@
<div>
<table id="myTable" class="table-striped">
<thead class="editheader">
<tr style="background-color: #003C71; color: white;">
<td class="" colspan="2" style="text-align:center;">Edit Details</td>
</tr>
</thead>
<tbody>
@foreach (var item in Model.displayAllTransactionsWithDetails)
{
@*<div id="bulkID" name="bulkID">
<tr>
<td>@Html.LabelFor(modelItem => item.bulkID, "Bulk ID", htmlAttributes: new { @class = "" })</td>
<td>@Html.DisplayFor(modelItem => item.bulkID, "", new { @class = "form-control text-box", @name = "" })</td>
</tr>
</div>*@
@if (Model != null && (item.SubType != null))
{
<div id="subTransactionID" name="subTransactionID">
<tr>
<td>@Html.LabelFor(modelItem => item.SubType, "Sub Type", htmlAttributes: new { @class = "" })</td>
<td>@Html.DisplayFor(modelItem => item.SubType, "", new { @class = "form-control textbox", @name = "" })</td>
</tr>
</div>
}
@if (Model != null && (item.customername != null))
{
<div id="customerName" name="customerName">
<tr>
<td>@Html.LabelFor(modelItem => item.customername, "Customer Name", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.customername, "", new { @class = "form-control textbox", @name = "" })</td>
</tr>
</div>
}
@if (Model != null && (item.GroupName != null))
{
<div id="groupName" name="groupName">
<tr>
<td>@Html.LabelFor(modelItem => item.GroupName, "Group Name", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.GroupName, "", new { @class = "form-control", @name = "EditModel.GroupName", onkeyup = "Expand(this);" })</td>
<td>@Html.HiddenFor(modelItem => item.GroupName)</td>
</tr>
</div>
}
@if (Model != null && (item.GroupNumber != null))
{
<div id="groupNumber" name="groupNumber">
<tr>
<td>@Html.LabelFor(modelItem => item.GroupNumber, "Group Number", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.GroupNumber, "", new { @class = "form-control", @name = "EditModel.GroupNumber", onkeyup = "Expand(this);" })</td>
<td>@Html.HiddenFor(modelItem => item.GroupNumber)</td>
<span asp-validation-for="@Model.GroupNumber"></span>
</tr>
</div>
}
@if (Model != null && (item.PlanCode != null))
{
<div id="planCode" name="planCode">
<tr>
<td>@Html.LabelFor(modelItem => item.PlanCode, "Plan Code", htmlAttributes: new { @class = "" })</td>
<td><div contenteditable="true">@Html.TextBoxFor(modelItem => item.PlanCode, "", new { @class = "form-control", @name = "Model.PlanCode", onkeyup = "Expand(this);" })</div></td>
</tr>
</div>
}
@if (Model != null && (item.PlanBenefitDescription != null))
{
<div id="planBenfefitDescription" name="planBenfefitDescription">
<tr>
<td>@Html.LabelFor(modelItem => item.PlanBenefitDescription, "Plan Benefit Description", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PlanBenefitDescription, "", new { @class = "form-control", @name = "Model.PlanBefitDescription", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.MemberNumber != null))
{
<div id="memberNumber" name="memberNumber">
<tr>
<td>@Html.LabelFor(model => item.MemberNumber, "Member Number", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(model => item.MemberNumber, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.EmployeeSSN != null))
{
<div id="employeeSSN" name="employeeSSN">
<tr>
<td>@Html.LabelFor(modelItem => item.EmployeeSSN, "Employee SSN", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.EmployeeSSN, "", new { @class = "form-control", @name = "item.EmployeeSSN", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.DependentSSN != null))
{
<div id="dependentSSN" name="dependentSSN">
<tr>
<td>@Html.LabelFor(modelItem => item.DependentSSN, "Dependent SSN", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.DependentSSN, "", new { @class = "form-control", @name = "item.DependentSSN", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.LastName != null))
{
<div id="lastName" name="lastName">
<tr>
<td>@Html.LabelFor(modelItem => item.LastName, "Last Name", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.LastName, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.FirstName != null))
{
<div id="firstName" name="firstName">
<tr>
<td>@Html.LabelFor(modelItem => item.FirstName, "First Name", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.FirstName, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.MiddleInitial != null))
{
<div id="middleInitial" name="middleInitial">
<tr>
<td>@Html.LabelFor(modelItem => item.MiddleInitial, "Middle Initial", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.MiddleInitial, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.Relationship != null))
{
<div id="relationship" name="relationship">
<tr>
<td>@Html.LabelFor(modelItem => item.Relationship, "Relationship", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.Relationship, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.DateOfBirth != null))
{
<div id="dateofBirth" name="dateofBirth">
<tr>
<td>@Html.LabelFor(modelItem => item.DateOfBirth, "Date of Birth", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.DateOfBirth, "{0:MM/dd/yyyy}", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.Gender != null))
{
<div id="gender" name="gender">
<tr>
<td>@Html.LabelFor(modelItem => item.Gender, "Gender", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.Gender, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.Address != null))
{
<div id="address" name="address">
<tr>
<td>@Html.LabelFor(modelItem => item.Address, "Address", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.Address, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.City != null))
{
<div id="city" name="city">
<tr>
<td>@Html.LabelFor(modelItem => item.City, "City", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.City, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.State != null))
{
<div id="state" name="state">
<tr>
<td>@Html.LabelFor(modelItem => item.State, "State", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.State, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.ZipCode != null))
{
<div id="zipCode" name="zipCode">
<tr>
<td>@Html.LabelFor(modelItem => item.ZipCode, "Zip Code", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.ZipCode, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.PCPStreetAddress != null))
{
<div id="pcpStreetAddress" name="pcpStreetAddress">
<tr>
<td>@Html.LabelFor(modelItem => item.PCPStreetAddress, "PCP Street Address", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PCPStreetAddress, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.PCPCity != null))
{
<div id="pcpCity" name="pcpCity">
<tr>
<td>@Html.LabelFor(modelItem => item.PCPCity, "PCP City", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PCPCity, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.PCPState != null))
{
<div id="pcpState" name="pcpState">
<tr>
<td>@Html.LabelFor(modelItem => item.PCPState, "PCP State", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PCPState, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.PCPZipCode != null))
{
<div id="pcpZipCode" name="pcpZipCode">
<tr>
<td>@Html.LabelFor(modelItem => item.PCPZipCode, "PCP Zip Code", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PCPZipCode, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.PCPID != null))
{
<div id="pcpID" name="pcpID">
<tr>
<td>@Html.LabelFor(modelItemel => item.PCPID, "PCP ID", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.PCPID, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.ProviderName != null))
{
<div id="providerName" name="providerName">
<tr>
<td>@Html.LabelFor(modelItem => item.ProviderName, "Provider Name", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(mmodelItemodel => item.ProviderName, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.MedicalIndicator != null))
{
<div id="medicalIndicator" name="medicalIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.MedicalIndicator, "Medical Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.MedicalIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.LifeIndicator != null))
{
<div id="lifeIndicator" name="liveIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.LifeIndicator, "Life Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.LifeIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.DentalIndicator != null))
{
<div id="dentalIndicator" name="dentalIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.DentalIndicator, "Dental Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.DentalIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.VisionIndicator != null))
{
<div id="visionIndicator" name="visionIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.VisionIndicator, "Vision Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.VisionIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.SupplementalLifeIndicator != null))
{
<div id="supplementalLifeIndicator" name="supplementalIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.SupplementalLifeIndicator, "Supplemental Life Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.SupplementalLifeIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.STDIndicator != null))
{
<div id="stdIndicator" name="stIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.STDIndicator, "STD Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.STDIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.LTDIndicator != null))
{
<div id="ltdIndicator" name="ltdIndicator">
<tr>
<td>@Html.LabelFor(modelItem => item.LTDIndicator, "LTD Indicator", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.LTDIndicator, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.TerminationDate != null))
{
<div id="terminationDate" name="terminationDate">
<tr>
<td>@Html.LabelFor(modelItem => item.TerminationDate, "Termination Date", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.TerminationDate, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.DateofHire != null))
{
<div id="dateofHire" name="dateofHire">
<tr>
<td>@Html.LabelFor(modelItem => item.DateofHire, "Date of Hire", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(momodelItemdel => item.DateofHire, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.MemberEffectiveDate != null))
{
<div id="memberEffectiveDate" name="memberEffectiveDate">
<tr>
<td>@Html.LabelFor(modelItem => item.MemberEffectiveDate, "Member Effective Date", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.MemberEffectiveDate, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.TransactionType != null))
{
<div id="transactionType" name="transactionType">
<tr>
<td>@Html.LabelFor(modelItem => item.TransactionType, "Transaction Type", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.TransactionType, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.QualifyingEventReason != null))
{
<div id="qualifyingEventReason" name="qualifyingEventReason">
<tr>
<td>@Html.LabelFor(modelItem => item.QualifyingEventReason, "Qualifying Event Reason(QER)", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.QualifyingEventReason, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.ExistingPatient != null))
{
<div id="existingPatient" name="existingPatient">
<tr>
<td>@Html.LabelFor(model => item.ExistingPatient, "Existing Patient", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(model => item.ExistingPatient, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.CurrentPlanMedicalEnrolled != null))
{
<div id="currentPlanMedicalEnrolled" name="currentPlanMedicalEnrolled">
<tr>
<td>@Html.LabelFor(modelItem => item.CurrentPlanMedicalEnrolled, "Current Plan Medical Enrolled", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.CurrentPlanMedicalEnrolled, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
@if (Model != null && (item.RenewalPlanMedicalSelection != null))
{
<div id="renewalPlanMedicalSelection" name="renewalPlanMedicalSelection">
<tr>
<td>@Html.LabelFor(modelItem => item.RenewalPlanMedicalSelection, "Renewal Plan Medical Selection", htmlAttributes: new { @class = "" })</td>
<td>@Html.TextBoxFor(modelItem => item.RenewalPlanMedicalSelection, "", new { @class = "form-control", onkeyup = "Expand(this);" })</td>
</tr>
</div>
}
}
</tbody>
</table>
<div>
<button type="submit" value="Save">Save</button>
</div>
</div>
</form>
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
//Gets all data for transaction by transID
displayAllTransactionsWithDetails = await _db.tblAllTransactionsWithDetails.Where(d => d.transID == id).ToListAsync();
return Page();
}
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
//Display Error Checking
if (!ModelState.IsValid)
{
return Page();
}
try
{
//var EditedDetailsModel =
var EditModelFromDb = await _db.tblAllTransactionsWithDetails.FindAsync(id);
EditModelFromDb.GroupName = GroupName;
EditModelFromDb.GroupNumber = GroupNumber;
EditModelFromDb.MemberNumber = MemberNumber;
EditModelFromDb.customername = customername;
await _db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EditModelExists(tblAllTransactionsWithDetails.transID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
