通过查询字符串中asp.net mvc的模式是否绑定工作字符串、绑定、模式、工作

由网友(我放弃你随意)分享简介:模式是否通过查询字符串结合的工作呢?Does model binding work via query string as well ?如果我有这样一个GET请求:If I have a get request like : GET /Country/CheckName?Country.Name=abc&Cou...

模式是否通过查询字符串结合的工作呢?

Does model binding work via query string as well ?

如果我有这样一个GET请求:

If I have a get request like :

GET /Country/CheckName?Country.Name=abc&Country.Id=0 HTTP/1.1

会在CountryController下面的方法有它从查询字符串,其中包含ID和名称的属性与价值oCountry说法?

Would the following method in CountryController have its oCountry argument containing Id and Name properties with values from the query string ?

public ViewResult CheckCountryName(Country oCountry)
{
     //some code
     return View(oCountry);
}

出于某种原因,我得到编号为0,名称作为oCountry对象为空。缺什么?

For some reason I am getting Id as 0 and Name as null in oCountry object. What is missing ?

推荐答案

是的,该模型绑定支持从查询字符串的结合。然而,同样的模型绑定的规则在这里也适用:该属性名称/ EX pressions应您的要求,并在模型相匹配。

Yes, the model binding supports binding from the query string. However the same model binding rules apply here also: the property names/expressions should match in your request and in your model.

所以,如果你有一个名称属性,那么你需要有一个名称键在查询字符串。如果你写 Country.Name 的模型绑定先来看看一个叫国家然后名称属性该国的对象。

So if you have a Name property then you need the have a Name key in the query string. If you write Country.Name the model binding first look for a property called Country and then a Name property on that country object.

所以,你不需要国家 preFIX你属性名,所以你的要求应该是这样的:

So you don't need the Country prefix for you property names, so your request should look like this:

/Country/CheckName?Name=abc&Id=1 HTTP/1.1

或者,如果你无法改变,你可以为你的操作参数指定的preFIX请求的 BindAttribute

public ViewResult CheckCountryName([Bind(Prefix="Country")]Country oCountry)
{
     //some code
     return View(oCountry);
}
阅读全文

相关推荐

最新文章