I have a simple model which models a vehicle entity.
It has attributes:
Model
Make
Colour
RegistrationPlate
On the create View I want to users to be able to perform a server look up using the registration plate to auto fill the Model/Make/Colour information.
As such I’m trying to create an action link like so:
@Html.ActionLink("Look up registration", "RegLookup", "Vehicle", Model.Registration)
Where RegLookup is a get ActionResult method in the vehicle controller which goes away and finds the model/make/colour information based on the registration field passed through to it. The Make/Model/Colour information is retrieved from a separate database.
However at present on rendering the view – before even getting to the action link – an exception is thrown indicating that the Model is Null.
What I want to be passed through is effectively the value from the textbox representing the registration:
@Html.EditorFor(model => model.Registration)
How do I pass this value through?
In your CreateView, typically your Model Properties should mostly be empty except some prepoulated values like dropdowns . What you should use is to use jQuery ajax to make an async call to an action method which returns the relevant data based on the registration plate and fill that in the Form
You may keep the action link like this
And attach a javascript function to the click event of it to make the ajax call
Assuming your
RegLookUpaction method accept a parameter (With nameregistration) and returns the data inJSONformat like below format