我的控制器方法如下:
public ActionResult SomeMethod(Dictionary<int, string> model) { }是否可以仅使用查询字符串来调用此方法并填充模型"?我的意思是,键入如下内容:
Is it possible to call this method and populate "model" using only query string? I mean, typing something like this:
ControllerName/SomeMethod?model.0=someText&model.1=someOtherText在我们的浏览器地址栏中.有可能吗?
in our browser address bar. Is it possible?
似乎我的问题被误解了-我想绑定查询字符串,以便自动填充Dictionary方法参数.换句话说-我不想在我的方法中手动创建字典,但是我会用一些自动数学的.NET绑定器来创建它,所以我可以像这样直接访问它:
It would appear my question was misunderstood - I want to bind the query string, so that the Dictionary method parameter is populated automatically. In other words - I don't want to manually create the dictionary inside my method, but have some automathic .NET binder do it form me, so I can access it right away like this:
public ActionResult SomeMethod(Dictionary<int, string> model) { var a = model[SomeKey]; }有没有自动装订机,足够聪明地做到这一点?
Is there an automatic binder, smart enough to do this?
推荐答案在ASP.NET Core中,可以使用以下语法(不需要自定义绑定程序):
In ASP.NET Core, you can use the following syntax (without needing a custom binder):
?dictionaryVariableName[KEY]=VALUE假设您使用此方法作为您的方法:
Assuming you had this as your method:
public ActionResult SomeMethod([FromQuery] Dictionary<int, string> model)然后调用以下URL:
?model[0]=firstString&model[1]=secondString您的字典将被自动填充.带有值:
Your dictionary would then be automatically populated. With values:
(0, "firstString") (1, "secondString")更多推荐
是否可以从查询字符串获取字典?
发布评论