If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState.Clear() to clean the ModelState values. The reason is that normally, you want to postback to the client the form with all the errors. So, when you put back the parameter that contain your...
Filed in: ASP.MVC
Did you know that you can know the original value written by the server to the client by calling: var originalName =document.getElementById("txtUserName").defaultValue; Yes, the Dom contain for every Html element of type HTMLInputElement and HTMLTextAreaElement the possibility to access the original value. This is interesting to know if...
Filed in: Javascript, Web
This situation occur if you have already the object inside the DbContext and you try to Attach your instance. Most of the time you can manage it but just remove the Attach method but in some case that you might not know if the instance is inside the DbContext you may would like to check it before attaching. This can be done with the help of DbSet and...
In a previous post concerning the IValidatableObject with Asp.Net MV3 we have discussed the powerful of the IValidatableObject interface with MVC framework which is a fast solution to handle error in the model, error in the model binding and error in the controller. What is great by using Microsoft ecosystem is that most of the framework work well...
They are a lot of way to validate model object in object oriented world. Most of the time, we see that we can check the information inside the Setter (which in C# is the SET of a property). This is interesting and personally my favorite way to do it. The main reason is that you never have any model object in a dirty state which could fail on future...
Filed in: ASP.MVC
You can compile any controller to have the same method name if they have different parameter type or numbers. This is normal in .Net framework but what MVC developer must know is that you cannot have 2 actions (which are methods) with the same name even if they doesn’t have the same parameter type. That mean you cannot have : public ActionResult...
Filed in: ASP.MVC
If you need to go inside ASP MVC code which can be very instructive you just need to hit 3 checkboxes inside Visual Studio and you will be ready to go. First of all, you need to go on the Debug>Options and Settings. This will open the Options of Visual Studio. From the Debug menu, you should already be in the good menu which is Debugging. Select the...
Sometime you may need to store information for few times in the memory without having to compute the data every time a user request it. This can save time and also processor time. To do, we can use the System.Web.Cache object. var cc = HttpRuntime.Cache.Get("Test"); DateTime t; if (cc == null) { t = DateTime.Now; HttpRuntime.Cache.Insert("Test",...
Filed in: ASP.MVC, ASP.Net, C#
Let say that you have an exception on the server side and you want to specify this error to the client, what could you do? The easiest way is to return the error into the return value directly: public JsonResult Create(MyObject myObject) { //AllFine return Json(new { IsCreated = True, Content = ViewGenerator(myObject)); //Error return Json(new...
Filed in: ASP.MVC, Javascript
It’s really easy to have in ASP.NET MVC a function that return an anonymous type. I say in ASP.NET MVC but this could be also in ASP.NET. In fact, when you have an action inside a controller that return a JsonResult you can simply return an anonymous type and Javascript will be able to handle it as simple as using the same syntax that you would...
Filed in: ASP.MVC, C#, dynamic
wordpress themplates