C# - TryUpdateModel Not Working With Prefix And IncludeProperties
Aug 9, 2010
i have an entity called User with 2 properties called UserName and Role (which is a reference to another entity called Role). I'm trying to update the UserName and RoleID from a form which is posted back. Within my postback action i have the following code:
var user = new User();
TryUpdateModel(user, "User", new string[] { "UserName, Role.RoleID" });
TryUpdateModel(user, new string[] { "User.UserName, User.Role.RoleID" });
However none of these updates the Role.RoleID property. If i try the following:
TryUpdateModel(user, "User", new string[] { "UserName, Role" });
TryUpdateModel(user);
The RoleID is updated but the RoleName property is validated aswell. That's why i'm trying to be more specific on which properties to update but i can't get any of the first examples to work.
I'm trying to redirect a domain to a sub-directory. It works for. [URL] but is not working for [URL]. Do you know what I need to modify in my web.config redirect code above to make [URL] work?
I have a view that displays a list of entities; I need to provide user a way to bulk-edit some properties of these entities. To do that I generate a form like the following:[Code]....
Next, I use javascript to submit user-entered values:[Code]....
Submitted data is processed by the following action:
[Code]....
Quantity and Price properties of the entities in the collection are updated as desired. However, all other properties are reset to null. AFAI, specifying the properties in "excludeProperties" list should prevent them from being changed by TryUpdateModel, right? What am I missing here?
I have following problem: in the Controller I call TryUpdateModel to send the changes made in the form back to the database. Then I save the entity, but no changes to database are made.
Here is the code from Controller:
[Code]....
Then the changes made to database. (ChangingSet have then also pending changes).
I have a strongly typed Edit view that expects a viewmodel class. On its post action, I want it to use TryUpdateModel to update the database with the new info. When debugging, TryUpdateModel returns true, but when I look in the database, nothing happened.
This is the controller Action
[Code]....
why this doesn't persist the changes back to the database?
I am following though the examples in Professional ASP.Net MVC 2 and one of the examples doesn't work for me.
[HttpPost] public ActionResult Edit (int id, FormCollection collection) { Dinner dinner = dinnerRepository.GetDinner(id); if (TryUpdateModel(dinner)) { dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerID }); } return View(new DinnerFormViewModel(dinner)); }
I understand that it's suppose to take the values from the FormCollection, and then update the dinner object with it, bit I don't see the collection get referenced anywhere.
I am trying to bind the dynamic object using TryUpdateModel(); but it works for first class member but it is not updating the properties of object which are referenced to model.
Since I upgraded from MVC 2 to MVC 3 RC, using TryUpdateModel causes a NullReferenceException. This problem only occurs when running my action method as part of a unit test. Running it on the actual server works as expected. Here's a stack trace of the exception:
System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.ValueProviderFactoryCollection.<>c_DisplayClassc.b_7(ValueProviderFactory factory) at System.Linq.Enumerable.WhereSelectEnumerableIterator[Code].... 2.MoveNext() at System.Collections.Generic.List[Code].... 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix) ... my own code from here on....
In case it matters, my controller has the following signature:
[Code]....
My guess is that this has to do with the new way DI works in MVC3, but I can't figure out what I'm doing wrong. Perhaps there is something in terms of DI setup that is required in MVC 3, but wasn't required in MVC 2?
when trying to update the db from the edit page, i would likt to take one field from the formcollection change things in it and update the model with the changed field.
I want to add a dynamic prefix to the url for some of the Views in my site, e.g. [URL] but am having difficulty. Out of the box I have [URL] but need to set a prefix dynamically. I created another Route in the Global.asax like so, but is is even needed? If it is how do I call it from my controller?
routes.MapRoute( "Questions", // Route name "{dyna}/{controller}/{action}/{id}", // URL with parameters new {dyna = "dyna", controller = "Quote", action = "Questions", id = UrlParameter.Optional } // Parameter defaults );
I have a C# application with a folder Controls (has three ascx files in it) in the same namespace. This is a sort of toolkit of reusable controls relating to this application. I currently have to register three different tag prefixes to use the controls but this seems messy. How can I reference all controls with a prefix such as mytoolkit:* where * is the name of the control I wish to use?
I Have many entries that have this 'HK' as a kind of prefix. in SQL Server table while i have nothing against HK, the sales person would like this HK removed. the name of this column is categoryname
I have MySqlMembershipProvider working with Asp.Net MVC. My question is how can I configure the table prefix... so instead of 'my_aspnet_' prefix on the tables, I want this to be either none or defined by me. My web.config:
I am trying to find the most efficient way to prefix listbox item text with their current index.I have two listboxes on a page, the first holds a list of states, the second holds the selection of states. When the user selects a state into the destination listbox, the state should be prefixed with the current index position. The destination box is sorted, and also has buttons to change the item positionsIs there a way to get the index of the item after insertion, instead of looping through the entire collection to add the index?
I am using a MaskedEditExtender on a textbox to verify a date. MaskType="Date" Mask="99/99/9999". Users of the system are used to typing just the last two digits of the year (i.e. 22/06/10) so I am trying to make this possible.
I have tried using Century="2000" but this option does not seem to function at all (still results in 22/06/0010).
A response in another forum suggests modifying the MaskedEditBehavior.js file to fix this behaviour - see here.
My question is, how should I go about modifying the MaskedEditBehavior.js file (as suggested in the link above), and how do I ensure that this modification is distributed with my application? I cannot even find this file to modify.
How to Create a function for Prefix generation on each insert row ?I have a table with ID as primary and is identity and increment by 1. and POID another column.Now I need to generate a function like this.In POID Column on each insert I need to set prefix like this
I have a details page containing a form field named UserId. On the same page i have another search form with a field also named UserId.
I am using Html.LabelFor(vm > vm.UserId) and Html.TextBoxFor(sm > sm.UserId) on the two different view models, vm being the view model and sm being the search model. (Yes, the UserId property on the two models has identical names - because they are the same domain property.
When i navigate to the page, the populated UserId on the vm is inserted into BOTH form fields named UserId by MVC. Even the sm.UserId are empty.
That is my initial problem. There are a few ways ti avoid that. My solution was to use the Prefix flag for the sm.
[code]...
My problem is that MVC can't map the Search.UserId (because of the .) to fit the UserId (prefixed with Search) in the action shown above.
So it seems like MVC has a prefix-feature, that are actually nok fully supported through the Route-handler.
Ofcourse i could rename the Search.UserId to Search_UserId, but then the name dosent match the name MVC expects in the recieving action above. (expects Search.UserId) Renaming The UserId property of the search model would fix the issue, but since it is the same value in the domain, this seems like a workaround.
how to remove master page prefix from control ID after rendering content page
i m using master page in my web application, i am using a java script that works on one of my div id and i am genrating div at run time using C# code, problem is that one one of my content page rendred a prefix ct100 attached on that div,
How can i remove that prefix from my server side control (assume panel) id