我创建了一个继承 KeyValuePair 的特殊用户控件。
在我的 ViewModel 中,有一个名为 Lookup 的属性
[UIHint("Lookup")]
public KeyValuePair<string, string> lookup { get; set; }
用户控制是
Html.TextBoxFor(m => m.Value, new { id = "Name", style = "width: 200px; background-color: #C0C0C0" })
Html.HiddenFor(m => m.Key, new { id="Guid"})
用户控件有一些设置 TextBox 和隐藏字段的值的 Jquery 语句。
当我对控制器的 POST 方法进行 DEBUG 时,我在 Lookup 属性中没有看到任何值?!
但是如果我将属性的类型更改为字符串而不是 KeyValuePair
并且还更改用户控件的类型,我看到一个值。
我想我已经很接近了,但我无法弄清楚。
The KeyValuePair
结构体没有默认的无参数构造函数,并且无法由模型绑定器实例化。我为您的视图推荐一个仅具有这些属性的自定义模型类。
public class CustomControlViewModel
{
public string Key { get; set; }
public string Value { get; set; }
}
将您的 KVP 转换为此模型类以供您查看和/或使用此类作为操作的参数。
[HttpGet]
public ActionResult Lookup()
{
return View( new CustomControlViewModel { Value = kvp.Value, Key = kvp.Key } );
}
[HttpPost]
public ActionResult Lookup( CustomControlViewModel lookup )
{
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)