I have created a special User Control which inherits KeyValuePair.
Inside my ViewModel, there is a property called lookup
[UIHint("Lookup")]
public KeyValuePair<string, string> lookup { get; set; }
User Control is
Html.TextBoxFor(m => m.Value, new { id = "Name", style = "width: 200px; background-color: #C0C0C0" })
Html.HiddenFor(m => m.Key, new { id="Guid"})
The user Control has some Jquery statements which set the value of the TextBox and the Hidden field.
When I do a DEBUG to the POST method of the Controller, I see no value inside the Lookup property?!
But If I changed the type of the property to string instead of KeyValuePair
and also change the type of the User Control, I see a value.
I think I’m very close but I can’t figure it out.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The KeyValuePair structure doesn’t have a default parameterless constructor and can’t be instantiated by the model binder. I recommend a custom model class for your view that has just those properties.
public class CustomControlViewModel
{
public string Key { get; set; }
public string Value { get; set; }
}
Transform your KVP into this model class for your view and/or use this class as the parameter on your action.
[HttpGet]
public ActionResult Lookup()
{
return View( new CustomControlViewModel { Value = kvp.Value, Key = kvp.Key } );
}
[HttpPost]
public ActionResult Lookup( CustomControlViewModel lookup )
{
...
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0