How can the textboxes pulling from the same properties have different values depending on if I use TextBoxFor or grab the propery directly off the model? The values in the text boxes that I created manually are the values that I expected. The values produced
by TextBoxFor appear to be from the model that was used for the action method that was called just before the one that returned this result.
Is this view being generated after an HTTP POST? If so then you might be getting the values that were in the original post submission. That's how it's supposed to work and is in place to support validation errors. In the case of a validation you want to
show the original values entered.
If you are rendering this view directly from an HTTP post, then consider putting it in an HTTP Get and redirecting to the new action. You can pass a model to the new action using TempData.
Also looking at your code if your using jquery mobile you need to use jquery ver. 1.6.4 due to some compatibility issues with jquery mobile and jquery 1.7.1. I agree with CodeHobo that could be your problem.
I know it's odd, but if you want to build those inputs for all items in the model your lambda needs to ignore the model (m) paramaters and capture the array directly. I normally use an array, so I've not tried it with a List<T>, so if ot doesn't work change
your loop to something like:
rsullivan
Member
3 Points
3 Posts
Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 24, 2012 09:38 PM|LINK
How can the textboxes pulling from the same properties have different values depending on if I use TextBoxFor or grab the propery directly off the model? The values in the text boxes that I created manually are the values that I expected. The values produced by TextBoxFor appear to be from the model that was used for the action method that was called just before the one that returned this result.
How can this view:
@model IList<MobileOrganizerMVC.Models.VehicleAnnouncement> <li id="announcementList"> <fieldset data-role="controlgroup"> <legend></legend> @for (int i = 0; i < Model.Count; i++) { <input name="@String.Format("[{0}].AnnouncementId", i)" type="text" value="@Model[i].AnnouncementId" /> <input name="@String.Format("[{0}].Announcement", i)" type="text" value="@Model[i].Announcement" /> @Html.TextBoxFor(m=>m[i].AnnouncementId) @Html.TextBoxFor(m=>m[i].Announcement) @Html.CheckBox(String.Format("[{0}].Selected", i), new { id = String.Format("[{0}].Selected", i), @class = "Announcement" }) <label for="@String.Format("[{0}].Selected", i)">@Model[i].Announcement</label> } </fieldset> </li><!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>PocketOrganizer</title> <meta name="viewport" content="height=device-height,width=device-width,initial-scale=1.0,maximum-scale=1.0" > <link rel="stylesheet" type="text/css" href="/Content/themes/base/jquery.mobile-1.0.1.min.css" /> <link rel="stylesheet" type="text/css" href="/Content/Site.css" /> <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).bind("mobileinit", function () { // As of Beta 2, jQuery Mobile's Ajax navigation does not work in all cases (e.g., // when navigating from a mobile to a non-mobile page, or when clicking "back" // after a form post), hence disabling it. $.mobile.ajaxEnabled = false; }); </script> <script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script> <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script> <script src="/Scripts/jquery.mobile-1.0.1.min.js" type="text/javascript"></script> <script src="/Scripts/VehicleScripts.js" type="text/javascript"></script> </head> <body> <div class="page"> <section id="main"> <li id="announcementList"> <fieldset data-role="controlgroup"> <legend></legend> <input name="[0].AnnouncementId" type="text" value="16" /> <input name="[0].Announcement" type="text" value="PUSHED" /> <input name="[0].AnnouncementId" type="text" value="1" /><input name="[0].Announcement" type="text" value="ABS LIGHT ON" /><input class="Announcement" id="[0].Selected" name="[0].Selected" type="checkbox" value="true" /><input name="[0].Selected" type="hidden" value="false" /> <label for="[0].Selected">PUSHED</label> <input name="[1].AnnouncementId" type="text" value="18" /> <input name="[1].Announcement" type="text" value="SALVAGE" /> <input name="[1].AnnouncementId" type="text" value="2" /><input name="[1].Announcement" type="text" value="ACTUAL MILES" /><input class="Announcement" id="[1].Selected" name="[1].Selected" type="checkbox" value="true" /><input name="[1].Selected" type="hidden" value="false" /> <label for="[1].Selected">SALVAGE</label> </fieldset> </li> </section> </div> </body> </html>CodeHobo
All-Star
18647 Points
2647 Posts
Re: Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 24, 2012 10:20 PM|LINK
Is this view being generated after an HTTP POST? If so then you might be getting the values that were in the original post submission. That's how it's supposed to work and is in place to support validation errors. In the case of a validation you want to show the original values entered.
If you are rendering this view directly from an HTTP post, then consider putting it in an HTTP Get and redirecting to the new action. You can pass a model to the new action using TempData.
Blog | Twitter : @Hattan
MasterV23
Member
113 Points
318 Posts
Re: Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 25, 2012 12:00 AM|LINK
bruce (sqlwo...
All-Star
36870 Points
5450 Posts
Re: Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 25, 2012 02:51 AM|LINK
I would guess that your list collection does not have a stable order, and Model[i] returns different item on each call.
rsullivan
Member
3 Points
3 Posts
Re: Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 25, 2012 02:31 PM|LINK
That was it. Thanks for the help.
BrockAllen
All-Star
27554 Points
4912 Posts
MVP
Re: Why does Html.TextBoxFor and and data pulled directly from model produce different values?
Feb 25, 2012 02:33 PM|LINK
Change your lambdas to this:
@Html.TextBoxFor(m=>Model[i].AnnouncementId)
I know it's odd, but if you want to build those inputs for all items in the model your lambda needs to ignore the model (m) paramaters and capture the array directly. I normally use an array, so I've not tried it with a List<T>, so if ot doesn't work change your loop to something like:
@{
var list = Model.ToArray();
for (var i = 0; i < list.Length; i++) {
@Html.TextBoxFor(x=>list[i].ProperyName)
}
}
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/