C# MVC 将字符串中的变量绑定到模型
C# MVC Bind variables in a string to model
在 C# MVC
中,您可以使用模型绑定自动将变量解析为模型。
public class RegistrationForm {
string Name {get;set;}
string Address {get;set;}
}
public ActionResult Register(RegistrationForm register) {
...
}
如果我传递 Name
和 Address
变量,它们可以直接在 register
对象中使用。
如果字符串中有变量,是否可以手动调用此绑定?
例如:
var s = "name=hugo&address=test";
//dosomething to get RegistrationForm register
//register.Name == hugo
我知道我可以用 HttpUtility.ParseQueryString(s);
得到一个 NameValueCollection
然后使用反射来得到 RegistrationForm
的属性并检查值是否存在,但我希望我可以使用MVC 使用的实际绑定方法。
MVC 绑定工作基于 属性 您的 ViewModel
名称(RegistrationForm
class)。
所以你完全正确,如果你使用 GET HTTP 方法从字符串绑定你的 属性 你可以直接这样写:
http://yourSite.com/YourController/Register?Name=hugo&Address=test
区分大小写,注意。
或者如果你使用 Razor 生成链接你可以写得更清楚:
@Url.Action("Register", new { Name = "hugo", Address = "test"})
你可以像这里一样模拟传递给模型绑定的 HttpContext
http://www.jamie-dixon.co.uk/unit-testing/unit-testing-your-custom-model-binder/
var controllerContext = new ControllerContext();
//set values in controllerContext here
var bindingContext = new ModelBindingContext();
var modelBinder = ModelBinders.Binders.DefaultBinder;
var result = modelBinder.BindModel(controllerContext, bindingContext)
您可以将字符串转换为 JSON 对象,然后您可以使用序列化程序将 JSON 对象解析为您的模型。
@Malcolm 的回答是我要求的,所以他得到了学分。但我最终还是用反思来做,因为在我看来它看起来更清晰,也更容易理解发生了什么。
var result = HttpUtility.ParseQueryString(strResponse);
Type myType = GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
try
{
prop.SetValue(this,
Convert.ChangeType(result[prop.Name], prop.PropertyType, CultureInfo.InvariantCulture),
null);
}
catch (InvalidCastException)
{
//skip missing values
}
catch (Exception ex)
{
//something went wrong with parsing the result
new Database().Base.AddErrorLog(ex);
}
}
免责声明
这对我有用,因为我只得到字符串和小数,不需要任何东西。这与 MVC 模型活页夹完全不同。
在 C# MVC
中,您可以使用模型绑定自动将变量解析为模型。
public class RegistrationForm {
string Name {get;set;}
string Address {get;set;}
}
public ActionResult Register(RegistrationForm register) {
...
}
如果我传递 Name
和 Address
变量,它们可以直接在 register
对象中使用。
如果字符串中有变量,是否可以手动调用此绑定? 例如:
var s = "name=hugo&address=test";
//dosomething to get RegistrationForm register
//register.Name == hugo
我知道我可以用 HttpUtility.ParseQueryString(s);
得到一个 NameValueCollection
然后使用反射来得到 RegistrationForm
的属性并检查值是否存在,但我希望我可以使用MVC 使用的实际绑定方法。
MVC 绑定工作基于 属性 您的 ViewModel
名称(RegistrationForm
class)。
所以你完全正确,如果你使用 GET HTTP 方法从字符串绑定你的 属性 你可以直接这样写:
http://yourSite.com/YourController/Register?Name=hugo&Address=test
区分大小写,注意。
或者如果你使用 Razor 生成链接你可以写得更清楚:
@Url.Action("Register", new { Name = "hugo", Address = "test"})
你可以像这里一样模拟传递给模型绑定的 HttpContext
http://www.jamie-dixon.co.uk/unit-testing/unit-testing-your-custom-model-binder/
var controllerContext = new ControllerContext();
//set values in controllerContext here
var bindingContext = new ModelBindingContext();
var modelBinder = ModelBinders.Binders.DefaultBinder;
var result = modelBinder.BindModel(controllerContext, bindingContext)
您可以将字符串转换为 JSON 对象,然后您可以使用序列化程序将 JSON 对象解析为您的模型。
@Malcolm 的回答是我要求的,所以他得到了学分。但我最终还是用反思来做,因为在我看来它看起来更清晰,也更容易理解发生了什么。
var result = HttpUtility.ParseQueryString(strResponse);
Type myType = GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
try
{
prop.SetValue(this,
Convert.ChangeType(result[prop.Name], prop.PropertyType, CultureInfo.InvariantCulture),
null);
}
catch (InvalidCastException)
{
//skip missing values
}
catch (Exception ex)
{
//something went wrong with parsing the result
new Database().Base.AddErrorLog(ex);
}
}
免责声明 这对我有用,因为我只得到字符串和小数,不需要任何东西。这与 MVC 模型活页夹完全不同。