i just noticed that if my controller has a parameter with a name that matches the name of a field in my view model, and the view emits the value of the model's field, the value emitted is that passed in the query string, not the one set by the controller. is
this by design?
here is what i mean:
my view model:
public class MyViewModel
{
int id;
string name;
}
my controller:
public ActionResult Create(int? id)
{
return View(new MyViewModel { id = 1, name = "test", });
}
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
thanks. it seems simlar yes and they have a workaround for it at least for routes and only for the case when the parameter is missing from the query string, but what do we do if the id (or any other parameter with the same name as in the model) is present
but we want to change it inside the controller?
Why not avoid the ambiguity and change the names? You can use the ActionName attribute to change the name of an action, but I don't know of a way to change conflicting parms/model names.
Just to clarify the previous answers:
Source data for model binding are read from "value provider" that (by default) uses route data, query string, POST data and "uploaded files data". So if route data or POST data item matches the model property name then it's always binded.
But ASP.NET MVC itself requires only two "fixed" names - "controller" and "action".
Don't forget to click "Mark as Answer" on the post that helped you.
Marked as answer by konsu on Feb 18, 2010 07:29 PM
konsu
Member
5 Points
20 Posts
query string parameters and view model fields
Feb 15, 2010 10:50 PM|LINK
hello,
i just noticed that if my controller has a parameter with a name that matches the name of a field in my view model, and the view emits the value of the model's field, the value emitted is that passed in the query string, not the one set by the controller. is this by design?
here is what i mean:
my view model:
public class MyViewModel
{
int id;
string name;
}
my controller:
public ActionResult Create(int? id)
{
return View(new MyViewModel { id = 1, name = "test", });
}
my view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyViewModel>" %>
...
<%=Html.HiddenFor(m => m.id)%>
...
sending a request to "create?id=1234" emits "1234" in the hidden field instead of "1".
why?
thanks for any help
konstantin
robert.weste...
Contributor
2352 Points
399 Posts
Re: query string parameters and view model fields
Feb 16, 2010 12:14 PM|LINK
Hi,
This sounds similar to what Phil Haack blogged about recently. Have a look at his post on Optional URL Parameters with ASP.NET MVC.
Best regards
Robert
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
konsu
Member
5 Points
20 Posts
Re: query string parameters and view model fields
Feb 16, 2010 12:48 PM|LINK
thanks. it seems simlar yes and they have a workaround for it at least for routes and only for the case when the parameter is missing from the query string, but what do we do if the id (or any other parameter with the same name as in the model) is present but we want to change it inside the controller?
konstantin
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: query string parameters and view model fields
Feb 16, 2010 04:42 PM|LINK
Why not avoid the ambiguity and change the names? You can use the ActionName attribute to change the name of an action, but I don't know of a way to change conflicting parms/model names.
konsu
Member
5 Points
20 Posts
Re: query string parameters and view model fields
Feb 16, 2010 05:45 PM|LINK
this is what i did. i changed names in my query string. i was just wondering why this is happening...
Augi
Contributor
6730 Points
1142 Posts
Re: query string parameters and view model fields
Feb 17, 2010 09:07 PM|LINK
Just to clarify the previous answers:
Source data for model binding are read from "value provider" that (by default) uses route data, query string, POST data and "uploaded files data". So if route data or POST data item matches the model property name then it's always binded.
But ASP.NET MVC itself requires only two "fixed" names - "controller" and "action".