ASP.NET MVC 3 binding user control of type KeyValuePair to ViewModel

后端 未结 1 848
挽巷
挽巷 2021-01-26 19:57

I have created a special User Control which inherits KeyValuePair. Inside my ViewModel, there is a property called lookup

[UIHint(\"Lookup\")]
public KeyValuePai         


        
相关标签:
1条回答
  • 2021-01-26 20:39

    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 )
    {
         ...
    }
    
    0 讨论(0)
提交回复
热议问题