I'm pretty new to this stuff, so I won't be surprised if I'm just way off on what to do. I'm working on an app that does the following: Pull Data into a Model -> Create Edit UI for Model -> Update Model with User Edits -> Save Model to DB -> Create Word Document Template Using Model.
I've done I have a working UI that fetches the data into a model and puts it into an editable UI using @HTML.TextBoxFor(m => m.item) and what not. Now I'm trying to use those html helpers to update the model with the user edits so that I can save the model and use it elsewhere. I can't seem to figure out how to update the model. Here is some additional context:
I have created a complex model that looks something like this:
public class DataModel
{
public int ID1 { get; set; }
public int ID2 { get; set; }
public List<ItemsModel> Items{ get; set; }
public List<StuffModel> Stuff{ get; set; }
public List<ThingsModel> Things{ get; set; }
}
I have a UI with a bunch of HTML helpers like: @Html.TextBoxFor(m => m.Items[3].itemname)
At the bottom of the UI, I have a button with an AJAX call attached to it
function updateData() {
let tableContainer = $("#generateDataContainer");
try {
tableContainer.empty();
//add spinner
tableContainer.html('<div class="spinner-border" role="status"> <span class="sr-only">Loading...</span> </div>')
$.ajax({
url: "/@ViewContext.RouteData.Values["controller"]/UpdateModelData",
cache: false,
type: "get",
data: {
},
contentType: "application/json",
dataType: "html",
success: function (result) {
tableContainer.empty();
tableContainer.html(result);
},
error: function (request, status, error) {
tableContainer.empty();
},
});
}
catch (e) {
console.log(e);
}
In the controller I have the following function:
public async Task<PartialViewResult> UpdateDataAsync()
{
DataModel model = new DataModel();
await TryUpdateModelAsync(model);
return PartialView("_Generate", model);
}
Currently, I'm just trying to display the model to verify changes using @Html.DisplayForModel(). Right now, the output that I'm getting is ID1 0 ID2 0 and nothing for the lists.
My first thought was that the TryUpdateModelAsync was failing, but when I tested this using an if statement, it returned true. So it seems that it is returning true without actually updating the model. What am I doing wrong? Thanks,