Reputation: 93
I have an OnPost method that is specific to updating a particular object by the name of Status:
public async Task OnPostStatus()
{
Status ExistingRecord = await _context.Status.FirstOrDefaultAsync(m => m.Id == Status.Id);
if (ExistingRecord == null)
{
_context.Status.Add(Status);
} else
{
ExistingRecord.Description = Status.Description;
ExistingRecord.Hint = Status.Hint;
_context.Attach(ExistingRecord).State = EntityState.Modified;
}
await _context.SaveChangesAsync();
StatusMessage = "Saved!";
await OnGetAsync();
}
But I actually do this exact same OnPost with 4 other objects that have the exact same properties. So, I want to make it more dynamic by passing in the type of object into the OnPost, like this:
public async Task OnPost(string objectName)
{
var objectName ExistingRecord = await _context.(objectName).FirstOrDefaultAsync(m => m.Id == (objectName).Id);
if (ExistingRecord == null)
{
_context.(objectName).Add((objectName));
} else
{
ExistingRecord.Description = (objectName).Description;
ExistingRecord.Hint = (objectName).Hint;
_context.Attach(ExistingRecord).State = EntityState.Modified;
}
await _context.SaveChangesAsync();
StatusMessage = "Saved!";
await OnGetAsync();
}
I don't know the specific syntax!
Upvotes: 1
Views: 682
Reputation: 6891
Since these classes have exactly same fields, you can create a common class
as the base class to store the fields.
In my example, there are three tables (tb1, tb2, tb3) and a base class (tbBase
),the base class is not related to database:
public class tbBase
{
public int Id { get; set; }
public string Description { get; set; }
public string Hint { get; set; }
}
public class tb1: tbBase
{
}
public class tb2 : tbBase
{
}
public class tb3 : tbBase
{
}
Then, from the view,take the tbBase class as the parameter
and pass it to the post method, as well as a specific table type parameter
that needs to be modified.
<form method="post">
Id:<input id="Id" type="text" name="Id" /><br/>
Description: <input id="Description" type="text" name="Description" /><br/>
Hint: <input id="Hint" type="text" name="Hint" /><br/>
table type:<input id="Text1" type="text" name="type" />
<input id="Button1" type="submit" value="button" />
</form>
In PageModel, store the key-value pairs of parameter table type and specific type through Dictionary
:
public async Task OnPost(tbBase table, string type)
{
Dictionary<string, Type> TableTypeDictionary = new Dictionary<string, Type>()
{
{ "tb1", typeof(tb1) },
{ "tb2", typeof(tb2) },
{ "tb3", typeof(tb3) }
};
dynamic obj = Activator.CreateInstance(TableTypeDictionary[type]);
for (int i = 0; i < obj.GetType().GetProperties().Length; i++)
{
obj.GetType().GetProperties()[i].SetValue(obj, table.GetType().GetProperty(obj.GetType().GetProperties()[i].Name).GetValue(table, null), null);
}
var ExistingRecord = _context.Find(TableTypeDictionary[type], obj.Id);
if (ExistingRecord == null)
{
obj.Id = 0;//ID is self growing, no need to customize settings
_context.Add(obj);
}
else
{
ExistingRecord.Description = table.Description;
ExistingRecord.Hint = table.Hint;
_context.Attach(ExistingRecord).State = EntityState.Modified;
}
await _context.SaveChangesAsync();
StatusMessage = "Saved!";
await OnGetAsync();
}
Upvotes: 1