There the account controller does contain anything like u have mentioned.
Hi,
Account Controller will use "Default" validation messages (that after RC2 you can change from Resource File). This is basic property validation, done by Default Binding.
And what I mentioned was a possible cause of the error you get bycalling ModelState.AddModelError without having a ModelValue. It's how MVC Framework works. I never said that you should make validation this way (I don't
do it, but you don't asked a suggestion :) ).
alberto.ferr...
Member
410 Points
97 Posts
Re: Html helper causing error
Mar 14, 2009 12:27 AM|LINK
Hi,
Account Controller will use "Default" validation messages (that after RC2 you can change from Resource File). This is basic property validation, done by Default Binding.
And what I mentioned was a possible cause of the error you get by calling ModelState.AddModelError without having a ModelValue. It's how MVC Framework works. I never said that you should make validation this way (I don't do it, but you don't asked a suggestion :) ).
Did your error still occur after you use Binding?
Again, read this post: http://forums.asp.net/t/1380609.aspx, and http://forums.asp.net/p/1377232/2900140.aspx
This kind of observations was already made. See second link.
(I agree with you, but ModelValue viability wasn't the question of SanjaySutar)
SanjaySutar
Participant
1128 Points
692 Posts
Re: Html helper causing error
Mar 16, 2009 05:27 AM|LINK
thanx a lot Alberto.
Finally it's working.
Here is the code
if
(Form["Name"].Trim().Length == 0){
ModelState.AddModelError("Name", "Name is required");
ModelState.SetModelValue("Name", Form.ToValueProvider()["Name"]); <<<--------- this line has done the trick
}
zullu
Member
18 Points
9 Posts
Re: Html helper causing error
Mar 17, 2010 04:23 PM|LINK
I understand the descriptions above and have tried to do the same way as SanjaySutar posted earlier.
But for some reason, I am still getting this error:
"The given key was not present in the dictionary."
on the line:
ModelState.SetModelValue("optionselected", formscoll.ToValueProvider()["optionselected"]);
This is my how my controller looks like: ----------------------------->
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult ModifyOptions(FormCollection formscoll)
{
if (formscoll["optionselected"] != null)
{
//Code for successfull updation.
}
else
{
ModelState.AddModelError("optionselected", "Must select an option.");
ModelState.SetModelValue("optionselected", formscoll.ToValueProvider()["optionselected"]);
//ModelState.SetModelValue("optionselected", ValueProvider["optionselected"]);
return View();
}
}
AND , This is my how my View looks like: ----------------------------->
<div id="optionsContainer">
<%= Html.RadioButton("optionselected", "1")%><img class="myImage" src="../Content/Images/1.gif" />
<%= Html.RadioButton("optionselected", "2")%><img class="myImage" src="../Content/Images/2.gif" />
<%= Html.RadioButton("optionselected", "3")%><img class="myImage" src="../Content/Images/3.gif" />
<%= Html.RadioButton("optionselected", "4")%><img class="myImage" src="../Content/Images/4.gif" />
</div>
<div><%= Html.ValidationMessage("optionselected")%></div>
====================================================
Now, looking at some other posts, I tried modifying this to:
ModelState.SetModelValue("optionselected", ValueProvider["optionselected"]);
instead of using ----- formscoll.ToValueProvider()["optionselected"] -----
then I get back the same initial error:
"Object reference not set to an instance of an object."
Any suggestion here.
Thanks,
Zullu