I have a entity that has a scalar string property “Clarified”.
Clarified can have to values, “Yes” or “No”.
In my ViewModel I have following properties.
public SelectList ClarifiedTypes { get; set; }
public string Clarified { get; set; }
Inside my Get action method I have done following:
model.ClarifiedTypes =
new SelectList(
new[] { new { Value = "1", Text = "Ja" }, new { Value = "2", Text = "Nej" }, },
"Value",
"Text");
This is how I make a DDL in my view:
<div class="editor-field">
@Html.DropDownListFor(x => x.ClarifiedTypes, Model.ClarifiedTypes, "Välj alternativ")
</div>
Now my question is inside the Post action method.
In some way I need to fill my Model.Clarified with the value that was selecting from Model.ClarifiedTypes and then do following ” entity.Clarified = Model.Clarified “. I have no idea how to do this.
I have tried:
Model.Clarified = Model.ClarifiedTypes.selectedvalue.ToString();
Goalcard.Clarified = model.Clarified
But I get an error:
No parameterless constructor defined for this object.
What I want to achieve is that users should be able to choose Yes or No value inside the DDL and then that value should get filled to my entity property.
Any help is appreciated!
Thanks in advance
Update:
I am recieving this error
o parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
You should write
And the selected value from drop down list will be into
Model.Clarifiedas you submit your formFor model binding to be able to work, your model should have default constructor, otherwise mvc won’t be able to create model and bind it.
If you cannot restore default constructor on your model, create custom model binder or better, create separate view model for it.