c#


Bind a model to a controller action with, and without, a specific constructor


I'm building a small website in C# using System.Web.Mvc. Currently we have some models, such as Model, that can be bound to normally ok, but there are times that we need to instantiate it via a constructor. To do this we make near duplicate objects, such as ModelParam, that gather all the model information, and let us build the model how we need. This however means we end up duplicating Model with ModelParam, and we're re-binding the model data to the models, via Model.Bind, whilst requiring the new data to not be the default value of that type.
class ModelParam {
public int Id { get; set; }
public int Name { get; set; }
}
class Model {
public Model(int id) {
Name = $"Id-{id}";
}
public int Id { get; set; }
public string Name { get; set; }
public void Bind(ModelParam modelParam) {
if (modelParam.Id != 0) {
Id = modelParam.Id;
}
if (modelParam.Name != null) {
Name = modelParam.Name;
}
}
}
class Controller : System.Web.Mvc.Controller {
public ActionResult A(Model model) {
// Work with model
}
public ActionResult B(ModelParam modelParam) {
var model = new Model(modelParam.Id);
model.Bind(modelParam)
// Work with model
}
}
How can I remove ModelParam, whilst still being able to specify how the model is made?
I thought about sub-classing System.Web.Mvc.DefaultModelBinder, so that we can change CreateModel, but can't think of a way to discern whether I should use the constructor or not. If this is a good idea, what's a good way to pass this information to the binder?
class ModelBinder : System.Web.Mvc.DefaultModelBinder {
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) {
if (/* instantiate by id */) {
var id = bindingContext.ValueProvider.GetValue("Id").RawValue;
return new Model(id);
}
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}

Related Links

DataTable Format
Reading all certificates from asp.net
MVC 5 Edit Action Saving Input Error
Get strongly typed Entity Framework entities using reflection
VSTO: Don't inlcude column header
When the mouse buttons are swapped in the OS, do the mouse click event handlers still work correctly with respect to which button was clicked?
TemplateSelector doesn't work
Entity Framework Fetch count of child entities as a property
most efficient method of loading images for image gallery page [closed]
Extend Code First Model with a navigation property
asp.net mvc 5 An exception of type 'System.ArgumentOutOfRangeException' occurred
Adjusted cosine similarity not working correctly
How to highlight the TreeviewItem in a TreeView to show the status?
How i can fixed the dataGridView Column index
How can I enable the tab focus in the header of a DataGridView?
Enumerator of value type of list in c#

Categories

HOME
max
oop
aix
dynamic
hex
azure-resource-manager
squeak
ms-application-insights
glympse
swap
browserstack
pyodbc
wkhtmltopdf
soap-client
angular-universal
kairosdb
box2d
background-image
aiml
project-structure
custom-component
gomobile
dev-c++
hololens
gnu-screen
inline-assembly
rvm
utc
emr
restangular
federated
ipython-parallel
deferred
textview
jmsserializerbundle
jcr
unity3d-5
ragel
repeat
perf
android-databinding
libconfig
google-cloud-powershell
flume-ng
qt-quick
gridgain
html-entities
angular-fullstack
rivets.js
extjs3
gige-sdk
tsc
breadcrumbs
portaudio
timefield
cedar-bdd
foreground
csc
normalisation
change-password
angularfire
archiva
computability
jsoncpp
roslyn-code-analysis
autoscaling
waterline
screen-lock
firebase-tools
nsq
caption
drools-guvnor
jsonmodel
growl
triplestore
yandex-api
mongoid4
surfaceview
static-class
msbuild-task
reference-manual
freeimage
bindinglist
scsf
file-processing
netduino
moss2007-security
lwp
norton

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile