i sometimes get an error with the UpdateModel function.
However, i can't seem to locate the exact message which field(s) is/are causing the problem.
For example this one:
"The model of type 'Enquete' was not successfully updated."
it has an innerexception of NULL, and no further description. Is there a way to find out what is causing the problem?
Michel
EDIT:
i see this in my output window:
"A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll"
Steve Sanderson has a wonderful article on using the MVC source to help you debug your application.
I think this will help you get more info.
Kindness,
Dan
Just a wild guess: You are trying to bind null to a value type. Check in your model for int, float, DateTime, structs, ... data types that you try to bind to null.
I was getting this type of exception and from my experience i got what was causing it...
We have one primary key field or identity field in the entity...
If we try to display that field in the view using html control like text box and all then at that time updateModel throws exception
otherwise it wont throw exception
Use TryUpdateModel instead. It won't throw exception, but then you can inspect ModelState for errors.
I know it is late but to help others.
Related
I am using asp.net MVC3 and I am very new to this technology.
My models are designed in such a way that the properties will throw validation errors if the data is invalid. In this case, the properties are not set with invalid data.
When I redisplay my editing-view, validation error messages are shown; however the values that the user previously entered are gone because the model that it is bound to only contains the old-valid data.
For example, say I had a Person class and the Name property cannot be a null or empty string otherwise it throws a validation exception and prevents the property from being set. Now say the user removes the value from the Name property and tries to save the Person from the web. A validation exception will be thrown and handled properly to add the error to the ModelState so that it is displayed on the screen; however the old value for the Name is redisplayed since the invalid, empty string never made it into the property.
I do not know how to solve this problem and any advice on the issue would be greatly appreciated.
My advise is allow invalid data but use validation attributes. You wont save invalid entities so there is no problem and this is the standard approach these days. If you don't want do that, there is no easy solution. Most simple solution would be using the info from Request.Form
You should implement IValidatableObject to performe this kind of validation at server side.
From MSDN IValidatableObject Interface:
Provides a way for an object to be invalidated.
Theres an exemple here Using IValidatableObject Custom Validation, also from MSDN.
The solution to this problem was to create a ViewModel that allowed invalid data to be entered into this. This solution also simplified my ModelBinder classes because it took on most of the work.
I made a DateTime part in Orchard, and when I try to edit the type that contains this DateTime part, I get the following exception. Below the exception is the code from cshtml file that is used for rendering part in the editor. Type of datetime is System.DateTime. The thing is that I get this exception sometimes, and sometimes it works perfectly, and I couldn't find any reason for getting this exception, like it is totally random.
System.InvalidCastException: Specified cast is not valid.
#model DateTimeP.Models.DateTimePart
<fieldset class="edit-datetime">
<legend>Datum i vreme događaja:</legend>
#Html.EditorFor(m => m.datetime)
</fieldset>
Does anyone know how to solve this problem, because it's been bugging me for some time?
Maybe you should reuse existing modules for that:
https://gallery.orchardproject.net/List/Modules/Orchard.Module.Contrib.DateTimeField
I have followed this tutorial for the most part to explain what I am doing. http://www.asp.net/data-access/tutorials/creating-a-business-logic-layer-vb
What i need to do is figure out the best way to approach to be able to update my formview. I do not understand what the tutorial is trying to explain to me so i tried it the way i have updated a gridview before. But I am receiving "No parameterless constructor defined for this object." I tried to debug and view the callstack but it does not really tell me much.
I have my sql stored procedure to update which when executed works fine.
I also have another class in which i reference the application details class
applicant.vb
This is the code in order for when you click the view details link on the gridview it passes you off to another page that shows that applicants details it is within the same applicant.vb class
I am trying to update using the following method on the .aspx page but i receive the following error "No parameterless constructor defined for this object."
Memberdetails.aspx
Without knowing which line of code is causing that error, I can't say for sure, however, my guess is that your error is on this line of code.
_applicantadapter = New applicantTableAdapter
Put an open parentheses after applicantTableAdapter to see the different constructor signatures available to you for that type. I bet you'll see none of the options allow no parameters.
That error means that the object type you are trying to instantiate requires that you include parameter(s) (and you are not).
I am storing an array of a custom serializable class in session on my site. When a page on the site changes, suddenly it renders them invalid, and tells me that it can't cast the type to it's own type. I assume the class version numbers are changing or something?!
I'd appreciate avoiding the "don't use session" answers, unless it's a really simple solution. I'm not trying to redesign this whole process.
Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ShipmentPackages[]' to type 'ShipmentPackages[]'.
Source Error:
Line 21: Else
Line 22: If Not Session("ShipmentList") Is Nothing Then
Line 23: ShipmentList = DirectCast(Session("ShipmentList"), ShipmentPackages()).ToList
Line 24: End If
Line 25: End If
I have seen this message a number of times myself, it is very annoying! As you pointed out, it probably because the assembly version changed. In Asp.Net, when the page changes, the code gets recompiled. Depending on where you put the class will determine if the class gets recompiled with the page or not. I would suggest moving any "model" type classes to a separate project. This will avoid this problem as well as the urge to mix view/controller and model code :).
You can also try serializing the object into session as XML. If you do, you should be able to deserialize it even if the assembly changes, though not if the properties on the object change.
I know you said you didn't want to hear this, but you might also consider not putting objects in the session. This makes it difficult to scale your application if the time ever comes that it is necessary. The sooner you fix this the easier it will be to fix.
A few days ago I got annoyed by this issue too. Alas Brian's first solution will only work as long as you do not need to compile the "model-project" again. If you do that (because of a bugfix, etc.) and update the running application (with users holding their session during the update-process, what is done in my case) you get the exception again :-(!
In my special case the best solution was really easy! I changed "DirectCast" to "TryCast". If the assemblyversion changed and casting fails, trycast will return nothing. In this case, or if I haven't written the dictionary/collection to the session yet, I fetch my data (again) over the database and store afterwards. The next time casting will work ;-)! And another great point, this works also if the interface of the object will change!
I'm trying to add a new ADO.Net Entity Data Model to an MVC project I am working on.
When I complete the wizard, choosing my db and tables (just a single table for now) I get an error ""Exception has been thrown by the target of an invocation." and it throws me back the add new item dialog.
At this point, an empty Data Model has been created in my project. If I then choose "Update Model From Database" and complete the wizard again, I get a similar error.
An exception of type 'System.Reflection.TargetInvocationException' occurred while attempting to update from the database. The exception message is: 'Exception has been thrown by the target of an invocation.'.
Any ideas? I have tried doing this in an empty project also and still no dice!
Alex
You could try creating the Entity Data Model manually using the EdmGen.exe tool. Hopefully that will give a bit more information about the error.
Try turning on the include exception faults behaviour
namespace DataService
{
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class mydataservice : DataService<DataContext>
{
blah blah
I had similar issue. Look for similar names and missing mapping of forgen keys
I have had the same exeption, in my case it was my db(MySql) that had a table without a primary key, I just added a column "ID" and made it the primary key and voila! the exeption was gone :)