0

I want to assign my url parameters to Model properties, passed as a parameter to the associated Action. For example;

Say, my url is http://www.example.com/Item/Index?color=red&size=50

My action inside the controller is like below:

public class ItemController : Controller
{
    public ActionResult Index(MyModel myModel)
    {
        //

        return View(myModel);
    }
}

I want to configure the model or whatever necessary so that my model takes the color and size as field values. The following didn't work:

public class MyModel 
{
    [Display(Name = "color")]
    public string Color{ get; set; }

    [Display(Name = "size")]
    public string Size{ get; set; }
}

What would be the correct way to solve the problem?

Thanks for any suggestion.

Update

Well, yes! The code above would work correctly, because Url parameter names are the same as model property names. I should explain my problem exactly as I encounter for the next time, sorry.

I must correct a part of my question to make it clear. The url should have been: http://www.example.com/Item/Index?c=red&s=50 to detect the problem.

If the url is like that, the code would not work. Because Url parameters don't have the same name as Model properties.

Updated model is below:

public class MyModel 
{
    [Display(Name = "c")]
    public string Color{ get; set; }

    [Display(Name = "s")]
    public string Size{ get; set; }
}
moztemur
  • 941
  • 1
  • 9
  • 22
  • It didn't work because the parameter to your action was of type MyModel and the type of your model you used was PageModel. – Erik Funkenbusch Apr 01 '15 at 20:24
  • What is the problem? Your code will assign the values to the properties of `MyModel` (but you don't do anything with the model - e.g. `return View(model);`) –  Apr 02 '15 at 00:00
  • My code does not assign the values. This is the problem. I can see the problem in debug mode. No need to use the model to see the problem. And this is not the point, that is, using the model or not. – moztemur Apr 02 '15 at 08:35
  • Of course it does. You obviously have other problems in your code if this is not working. –  Apr 02 '15 at 22:27
  • @Stephen Muecke You're right. It would work. I have updated the question. – moztemur Apr 06 '15 at 12:11
  • 1
    In that case, make the method `public ActionResult Index(string c, string s)` and initialize a new instance of `MyModel` using those values, or change the name of the properties - `public string C { get; set; } public string S { get; set; }` –  Apr 06 '15 at 12:18
  • I have picked the latter. Thanks a lot. – moztemur Apr 06 '15 at 12:20

1 Answers1

0

Try adding [FromUri] in front of the parameter.

public class ItemController : Controller 
{ 
   public ActionResult Index([FromUri] MyModel myModel) 
   { 
        // do something 
       return View(); 
   } 
}

debugging the issue

Here are some suggestions in debugging the issue, as it should work out of the box.

  • try binding to primitive types

    public class ItemController : Controller 
    { 
           public ActionResult Index(string color, string size) 
           { 
                // do something 
               return View(); 
           } 
    }
  • Try reading out of the request object directly

    var size = this.Request["size"];

If either of those work there is an issue with your model binding.

Update

If you want to have the query string parameters different to the model in MVC you'll need to have a custom model binder. Take a look at Asp.Net MVC 2 - Bind a model's property to a different named value and http://ole.michelsen.dk/blog/bind-a-model-property-to-a-different-named-query-string-field.html which extends the answer a little.

https://github.com/yusufuzun/so-view-model-bind-20869735 has an example with some html helpers that could be useful.

Community
  • 1
  • 1
JonSquared
  • 683
  • 6
  • 19
  • OK. I must admit it is a carelessly asked question. Actually, the trick that I want to point is how to get the parameters from URL to the Model. There is no problem in hitting the Action method in debug mode. – moztemur Apr 01 '15 at 20:34
  • @mhmtztmr I thought that might have been the reason but good to point out the obvious errors first. I've updated my answer accordingly. – JonSquared Apr 01 '15 at 20:48
  • @mhmtztmr your example should work out of the box. You can try to update your question with more accurate code or provide project that reproduces the problem. I've added some things you might try to narrow where the problem is. – JonSquared Apr 04 '15 at 11:35
  • I have updated my question to the exact form where I encountered the problem. – moztemur Apr 06 '15 at 12:13