I apologize if these questions have already been asked and I may not fully understand the Model State process. I do understand that ModelState is used with validation. After some searching I see that ModelState.Clear() is not a good fix and I need to correct
my application. It is also recommended to use the get- redirect –post process.
I have a details view that uses the Displayfor helpers and I have an action link that directs the user to an edit view. The details view shows a one to many relationship and the selected item is sent by using the action link. The controller calls the model
class and retrieves basically a single class. These classes have an Id property which does not get changed when the [get] action result is used. To update the id to the correct value I have to clear the model state dictionary.
So the questions are: Do I use an [post] action result and find the selected item then redirect the user to the [get] edit action result? This seems to be more unnecessary validation processing than just sending the request through a link. Should I not
use the Displayfor helpers? This seems dumb not to take advantage of these helpers. I would think that an action link would be considered as a redirect and the Model State would know that it does not need to validate a display for helper.
In your controller's [HttpGet] method, you simply create a new instance of your Model class and pass it to the View.
In my code I name this class: data and pass it to the View: View( data );
In your controllers [HttpPost] method you can call ModelState.IsValid to make sure that the Model is valid before acting upon it.
Now it is possible to pass data to the initial view with your Model class but that requires settting properties on your class before passing it to the view.
In the [httpGet] I do return a new instance but it does not update the Id.
In order to change the id I have to clear the value from the dictionary. The id comes in as 2 and the after I get the new instance,
which has the value of 3 the value of 2 is stored in the view. This value is stored as an hiddenfor helper. I confirmed this by looking at the brower's(firefox) view source.
Also the details view, where the data is first viewed, is only using the Displayfor html helper without a validation helper.
@Html.ActionLink("Edit",
"Edit",
"Reviews",
new
{ id = item.RestaurantNumber, reviewid = item.Id }, null)
publicActionResult
Edit(int
id = 1, int
reviewid = 0) {
ModelState.Clear()
// The wrapper class is where I get the data which is using linq to sql
var
_review = newWrapperClass().LocateReviews(id,
reviewid).Single();
ModelState.Clear(); simply removes all items from the ModelState Dictionary. I'm not sure why you think you need to do that. Maybe you were thinking that it would clear the Model itself?
Are you sure that the true type of the _review is what your view is expecting?
Remember it is the Properties of the Model class that MVC is using to match up with the controls in a View. I suspect the your var _reivew doesn't have any Properties defined.
The only way to update the id is clearing it from the dictionary and then the id is updated with the correct value. My suspicion is that the view is using the id from the dictionary because it thinks that this is
equivalent to an [httppost] or the interface maybe part of the confusion.
I tried to use the ScaffoldColumn = false but I need to get the id value when I update the table's values. The _review is strongly-typed to the view and that is what I expect. I also have an interface for this id so I can use a generic constraint for an
extension method. This id is the primary key for my table. There is a one to many relationship so in order to find the specific review I have to use this id or I end up with a lousy work around with in the update method. I have read that our developers have
issues with the hiddenfor helper. I am trying to find a apporpriate solution. Thanks again for looking at this.
When I comment out ModelState.Clear() the value from the dictionary is passed to the view which is 2
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="2"/>
When I use ModelState.Clear() the value from _review is used.
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="3" />
These input fields are from Firefox's view source. This confirms that ModelState.Clear() is controlling the id field value.
Thanks for your reply. Already tried that and I placed the Id in a seperate MapRoute in Global.asax. Not sure if the url parameter is case senitive but I did temporarly changed it. Also when I added the MapRoute to the Edit the Actionlink bypassed the details
action result and I had to add that the the MapRoute. I know that the ModelState solves the issue but it is not an
eloquent solution.
Thank you gentleman for your assistance. I finally figured out a simple fix for this. I changed the parameter name from Id to something more specific such as DetailsId which the
dictionary is no longer overriding. The kiss theory was in play for this issue.
Sherman46
Member
13 Points
12 Posts
MVC 3 Model State
Feb 18, 2012 03:29 AM|LINK
I apologize if these questions have already been asked and I may not fully understand the Model State process. I do understand that ModelState is used with validation. After some searching I see that ModelState.Clear() is not a good fix and I need to correct my application. It is also recommended to use the get- redirect –post process.
I have a details view that uses the Displayfor helpers and I have an action link that directs the user to an edit view. The details view shows a one to many relationship and the selected item is sent by using the action link. The controller calls the model class and retrieves basically a single class. These classes have an Id property which does not get changed when the [get] action result is used. To update the id to the correct value I have to clear the model state dictionary.
So the questions are: Do I use an [post] action result and find the selected item then redirect the user to the [get] edit action result? This seems to be more unnecessary validation processing than just sending the request through a link. Should I not use the Displayfor helpers? This seems dumb not to take advantage of these helpers. I would think that an action link would be considered as a redirect and the Model State would know that it does not need to validate a display for helper.
eric2820
Contributor
2777 Points
1161 Posts
Re: MVC 3 Model State
Feb 18, 2012 03:43 PM|LINK
In your controller's [HttpGet] method, you simply create a new instance of your Model class and pass it to the View.
In my code I name this class: data and pass it to the View: View( data );
In your controllers [HttpPost] method you can call ModelState.IsValid to make sure that the Model is valid before acting upon it.
Now it is possible to pass data to the initial view with your Model class but that requires settting properties on your class before passing it to the view.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 18, 2012 04:12 PM|LINK
Eric,
Thank you for replying.
In the [httpGet] I do return a new instance but it does not update the Id.
In order to change the id I have to clear the value from the dictionary. The id comes in as 2 and the after I get the new instance,
which has the value of 3 the value of 2 is stored in the view. This value is stored as an hiddenfor helper. I confirmed this by looking at the brower's(firefox) view source.
Also the details view, where the data is first viewed, is only using the Displayfor html helper without a validation helper.
@Html.ActionLink("Edit", "Edit", "Reviews", new { id = item.RestaurantNumber, reviewid = item.Id }, null)
public ActionResult Edit(int id = 1, int reviewid = 0) {
ModelState.Clear()
// The wrapper class is where I get the data which is using linq to sql
var _review = new WrapperClass().LocateReviews(id, reviewid).Single();
return View(_review); }
eric2820
Contributor
2777 Points
1161 Posts
Re: MVC 3 Model State
Feb 18, 2012 06:49 PM|LINK
ModelState.Clear(); simply removes all items from the ModelState Dictionary. I'm not sure why you think you need to do that. Maybe you were thinking that it would clear the Model itself?
Are you sure that the true type of the _review is what your view is expecting?
Remember it is the Properties of the Model class that MVC is using to match up with the controls in a View. I suspect the your var _reivew doesn't have any Properties defined.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 18, 2012 07:35 PM|LINK
The only way to update the id is clearing it from the dictionary and then the id is updated with the correct value. My suspicion is that the view is using the id from the dictionary because it thinks that this is equivalent to an [httppost] or the interface maybe part of the confusion.
I tried to use the ScaffoldColumn = false but I need to get the id value when I update the table's values. The _review is strongly-typed to the view and that is what I expect. I also have an interface for this id so I can use a generic constraint for an extension method. This id is the primary key for my table. There is a one to many relationship so in order to find the specific review I have to use this id or I end up with a lousy work around with in the update method. I have read that our developers have issues with the hiddenfor helper. I am trying to find a apporpriate solution. Thanks again for looking at this.
public class Reviews : IRestaurants
{
public int Id { get; set; }
public int Rating { get; set; }
public string Comments { get; set; }
DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode=true)]
public DateTime DateCreated { get; set; }
public int RestaurantNumber { get; set; }
}
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 18, 2012 08:03 PM|LINK
ahmadhamdan1...
Member
125 Points
32 Posts
Re: MVC 3 Model State
Feb 18, 2012 08:23 PM|LINK
try renaming ID to a different name and use that name
because i think your propblem with the Id is related for it used i nthe routing code in global.asax
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 18, 2012 08:40 PM|LINK
Ahnadhamdan,
Thanks for your reply. Already tried that and I placed the Id in a seperate MapRoute in Global.asax. Not sure if the url parameter is case senitive but I did temporarly changed it. Also when I added the MapRoute to the Edit the Actionlink bypassed the details action result and I had to add that the the MapRoute. I know that the ModelState solves the issue but it is not an eloquent solution.
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 18, 2012 09:11 PM|LINK
The Model Metadata confirms that I am passing the correct types.
Sherman46
Member
13 Points
12 Posts
Re: MVC 3 Model State
Feb 22, 2012 03:59 PM|LINK
Thank you gentleman for your assistance. I finally figured out a simple fix for this. I changed the parameter name from Id to something more specific such as DetailsId which the dictionary is no longer overriding. The kiss theory was in play for this issue.