FormData hat Werte, wird aber am Endpunkt als Null empfangen. Warum?C#

Ein Treffpunkt für C#-Programmierer
Guest
 FormData hat Werte, wird aber am Endpunkt als Null empfangen. Warum?

Post by Guest »

Code: Select all

   let itemIndex = 0, goalIndex = 0;
tableCollection.forEach((table) => {
//GOALS
const thead = table.querySelector("thead");
const tr = thead.querySelector(".top-header");
const uid = table.getAttribute("data-bs-temp-id");
const goalID = table.getAttribute("data-bs-goal-id");
if (tr) {
const categoryID = tr.getAttribute("data-bs-category-id");
const goalName = tr.querySelector('input[data-bs-column="GoalName"]');
const percentageTD = tr.querySelector('input[data-bs-column="Percentage"]');
const isItemsAutoCompute = tr.querySelector('input[data-bs-column="IsItemsAutoCompute"]');

if (goalName && percentageTD && isItemsAutoCompute) {
const percentage = percentageTD.value.replace("%", '').trim();
const category = categories.find(x => x.CategoryID == categoryID);
const performanceID = category && category.IsForAll ? 0 : (parseInt(hdperformanceID.value) || 0);
const itemAuto = isItemsAutoCompute?.checked ?? false;

formData.append(`vm.Goals[${goalIndex}].TempID`, uid || '');
formData.append(`vm.Goals[${goalIndex}].GoalID`, parseInt(goalID) || 0);
formData.append(`vm.Goals[${goalIndex}].PerformanceID`, performanceID);
formData.append(`vm.Goals[${goalIndex}].CategoryID`, parseInt(categoryID) || 0);
formData.append(`vm.Goals[${goalIndex}].GoalName`, goalName.value);
formData.append(`vm.Goals[${goalIndex}].GoalPercentage`, parseFloat(percentage || 0));
formData.append(`vm.Goals[${goalIndex}].IsItemsAutoCompute`, itemAuto);

goalIndex+=1;
}
}

//ITEMS//
let Items = [];
const tbody = table.querySelector("tbody");
if (tbody) {
const tRows = tbody.querySelectorAll("tr");
tRows.forEach((row) => {
const itemID = parseInt(row.getAttribute("data-bd-item-id")) || 0;
const iUid = row.getAttribute("data-bd-temp-item-id") || "";
formData.append(`vm.Items[${itemIndex}].ParentTempID`, uid);
formData.append(`vm.Items[${itemIndex}].TempItemID`, iUid);
formData.append(`vm.Items[${itemIndex}].GoalID`, parseInt(goalID) || 0);
formData.append(`vm.Items[${itemIndex}].ItemID`, itemID);

if (columns) {
for (const column of columns) {
let colName = column?.ColumnName?.trim();
if (colName) {
const data = row.querySelector(`td[data-bs-column="${colName}"]`);
if (!data) continue;

colName = colName.toLowerCase();

switch (colName) {
case "itemname":
if (!data.textContent.trim()) {
goSave = false;
message = "Please enter the item name.";
data.focus();
return;
}
formData.append(`vm.Items[${itemIndex}].ItemName`, data.textContent.trim());
break;

case "itemweight":
if (!itemauto &&  !data.textContent.trim()) {
goSave = false;
message = "Please enter the item weight.";
data.focus();
return;
}
const itemWeight = parseFloat(data.textContent.replace("%", "").trim()) || 0;
formData.append(`vm.Items[${itemIndex}].ItemWeight`, itemWeight);
break;

case "unit":
const select = data.querySelector("select");
if (select) {
if (select?.value  0) {
companies.forEach((company, cIndex) => {
const companyName = company.CompanyName.toLowerCase().replace(/\s+/g, '');
if (colName.includes(companyName)) {
formData.append(`vm.Items[${itemIndex}].CompanyTargets[${cIndex}].CompanyID`, company.CompanyID.toString());
formData.append(`vm.Items[${itemIndex}].CompanyTargets[${cIndex}].ItemID`, itemID);

if (configuration.IsOngoing && configuration.KRAEnabled) {
if (colName.toLowerCase().includes("-tgt")) {
formData.append(`vm.Items[${itemIndex}].CompanyTargets[${cIndex}].Target`, data.textContent || "");
}
} else if (configuration.IsOngoing &&  configuration.AppraisalEnabled) {
if (colName.toLowerCase().includes("-rst")) {
formData.append(`vm.Items[${itemIndex}].CompanyTargets[${cIndex}].Result`, data.textContent || "");
}
}
}
});
}
}
break;
}
}
}
}

const actionData = row.querySelector(`td[data-bs-column="Action"]`);
if (actionData) {
const uploadElement = actionData.querySelector("input[type='file']");
if (uploadElement?.files?.length > 0) {
Array.from(uploadElement.files).forEach((file) => {
formData.append(`vm.Items[${itemIndex}].Attachments`, file);
});
}
}

itemIndex++;
});
}
});

[HttpPost, Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task JSUpdateEvaluationItems([FromForm]PerformanceViewerViewModel vm)
{
var result = await performanceRepository.UpdateEvaluationItems(vm);
return Json(result.ToString());
}
Dies ist mein Code zum Anhängen meiner Daten an Formulardaten und wird an einen Endpunkt gesendet.
Ich habe meine formData überprüft und sie haben Werte . Beim Empfang am Endpunkt ist der Parameter vm jedoch null.
Als ich nachgeforscht habe, als ich diesen Teil des Codes entfernt habe
formData.append( vm.Items[${itemIndex}].CompanyTargets[${cIndex}].CompanyID, company.CompanyID);
Die VM hat Werte.
Was könnte der mögliche Grund dafür sein? Wie kann ich das beheben
BEARBEITEN: Ich habe die gesamte Verarbeitung der Daten von tableCollections hinzugefügt.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post