Does model binding work via query string in asp.net mvc

前端 未结 1 934
抹茶落季
抹茶落季 2020-12-10 02:10

Does model binding work via query string as well ?

If I have a get request like :

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


        
1条回答
  •  醉梦人生
    2020-12-10 02:57

    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.

    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.

    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
    

    Or if you cannot change the request you can specify the prefix for your action parameter with the BindAttribute:

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

    0 讨论(0)
提交回复
热议问题