I am doing an online store project on ext.net. In my controller there is a method by which some fields of my entity-database should be edited. The number of these fields is not known in advance, because the handler that processes the database based on the request draws these fields in the view based on the enumeration of the classes containing these fields. After that, I click on the button and get the data from all the fields, then pass it to the Edit function as a FormCollection.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(FormCollection formcol)
{
var list = fieldManager.Edit(new FieldEditParams
{
ModelsForEdit = new List<Type> { typeof(Item), typeof(Laptop), typeof(Smartphone) },
InputCollection = formcol,
DbContext = db
});
return this.Direct();
}
The fields passed to the function may belong to different database tables. They are presented in the following form:
Key: "Id", Value: "here_is_item_guid"
Key: "Name", Value: "Laptop name"
The Edit method called in the controller:
public List<string> Edit(FieldEditParams @params)
{
var formColKeys = @params.InputCollection.AllKeys.Where(x => x != "__RequestVerificationToken").ToList();
var formColValues = formColKeys.Select(x => @params.InputCollection[formColKeys.IndexOf(x)]).ToList();
var formColValKeys = formColKeys.Zip(formColValues, (key, value) => new { key, value }).ToDictionary(x => x.key, x => x.value);
}
At this point, I have to process these fields, the number of which is not predetermined, because I can submit absolutely any model as a parameter to the input, and add these fields to the database, or rather, edit existing fields, receiving the goods of the online store with using the Id-field.
The downside is that the same fields can get caught, and they will be in different tables. Then it will not be clear which field needs to be updated. I can solve this problem, for example, FormCollection data will now look like this:
Key: "LaptopId", Value: "here_is_item_guid"
Key: "LaptopName", Value: "Laptop name"
Key: "SmartphoneId", Value: "here_is_item_guid"
Key: "SmartphoneName", Value: "Laptop name"
...where Laptop and Smartphone are the names of the database tables.
I used to do database editing like this:
var item = db.Item.FirstOrDefault(x => x.Id == model.Id);
item.Name = model.Name;
item.Laptop = new Laptop
{
Cpu = model.Cpu,
};
db.SaveChanges();
But now this solution is impossible, because I can’t access the database table simply through a dot, like this:
db.Item.
I thought it was possible to do something like this:
db["Items"][formColValKeys[1]] = "updating of name field in items";
db["Items"]["Laptops"][formColValKeys[1]] = "updating of name field in laptops";
FieldEditParams is a class like this:
public class FieldEditParams
{
public List<Type> ModelsForEdit { get; set; }
public FormCollection InputCollection { get; set; }
public AppDbContext DbContext { get; set; }
}
It has a database context, types of database table classes and FormCollection with all the data. I can’t use @Html.EditorForModel() or X.TextFieldFor(), because in the view written in ext.net in my case there is no html code, and I can’t use the model, the data should be obtained only using ajax requests, which I do . Therefore, I have to generate fields on the controller side, and in order not to prescribe all the fields of many tables manually, I decided to make a handler that can process, display and allow editing any tables.
And I almost made it, it remains to add database editing. I need your help with this. If I missed or indicated something, or my wording is not accurate, write about it in the comments, ask questions, and I will correct my post.
Aucun commentaire:
Enregistrer un commentaire