URL encoded colon resolves in 400 Bad Request

It seems that ASP.net does not allow colons before the ‘?’ in an URL, even if it is encoded as %3A. For example, these won’t work http://foo.org/api/persons/foo:bar http://foo.org/api/persons/foo%3abar But this works: http://foo.org/api/persons?id=foo%3abar In all examples, we would expect ASP.NET MVC to pass “foo:bar” as an id argument, properly decoded. I just tested this with MVC4 … Read more

First html helper generates client-side validation attributes, while the second one doesn’t

The CheckBox() helper does not render thedata-val attributes because the form has already rendered CheckBoxFor() for the same property. If you swap the order, the data-val attributes would be rendered for CheckBox() (and not for CheckBoxFor()). My understanding is this would cause a potential (duplication) problem with jquery.validation.unobtrusive when parsing the form. The html helpers … Read more

Jquery post and unobtrusive ajax validation not working mvc 4

You are calling your post via ajax so you will need to manually call $form.validate(); and test the result with $form.valid(): function SaveCity() { $.validator.unobtrusive.parse($form); $form.validate(); if ($form.valid()) { $.ajax({ type: “POST”, url: “/Home/SaveCity”, contentType:”application/json; charset=utf-8″, data: { Id: $(‘.cityId’).val(), City: $(‘.cityName’).val() }, success: function (data) { } }); } } If it is purely … Read more

ASP.NET MVC 4 JSON Binding to the View Model – Nested object error

You can keep your existing ActionMethod untouched without the need of json serializing: In the client side create an object from your json: JSON.parse(jsonData) and send that in the $.ajax data property. Or, instead of creating json, create an object: var dataObject = new Object(); dataObject.Town = $(‘#txt-Town’).val(); dataObject.District = $(‘#txt-District’).val(); … And again, send … Read more

ASP.NET MVC Partial view ajax post?

Don’t redirect from controller actions that are invoked with AJAX. It’s useless. You could return the url you want to redirect to as a JsonResult: [HttpPost] public ActionResult _AddCategory(CategoriesViewModel viewModel) { if(//success) { // DbOperations… return Json(new { redirectTo = Url.Action(“Categories”) }); } else { // model state is not valid… return PartialView(viewModel); } } … Read more

How to download a file through ajax request in asp.net MVC 4

I think there is no need of Ajax call you can do simply using hyperlink as below example. View Code <a href=”https://stackoverflow.com/questions/30704078/@Url.Action(“DownloadAttachment”, “PostDetail”, new { studentId = 123 })”>Download Form</a> Controller Method public ActionResult DownloadAttachment(int studentId) { // Find user by passed id var file = db.EmailAttachmentReceived.FirstOrDefault(x => x.LisaId == studentId); byte[] fileBytes = System.IO.File.ReadAllBytes(file.Filepath); … Read more

Problems implementing ValidatingAntiForgeryToken attribute for Web API with MVC 4 RC

You could try reading from the headers: var headers = actionContext.Request.Headers; var cookie = headers .GetCookies() .Select(c => c[AntiForgeryConfig.CookieName]) .FirstOrDefault(); var rvt = headers.GetValues(“__RequestVerificationToken”).FirstOrDefault(); AntiForgery.Validate(cookie != null ? cookie.Value : null, rvt); Note: GetCookies is an extension method that exists in the class HttpRequestHeadersExtensions which is part of System.Net.Http.Formatting.dll. It will most likely exist in … Read more

MVC 4 ignores DefaultModelBinder.ResourceClassKey

This is not specific to ASP.NET MVC 4. It was the same in ASP.NET MVC 3. You cannot set the required message using DefaultModelBinder.ResourceClassKey, only the PropertyValueInvalid. One way to achieve what you are looking for is to define a custom RequiredAttributeAdapter: public class MyRequiredAttributeAdapter : RequiredAttributeAdapter { public MyRequiredAttributeAdapter( ModelMetadata metadata, ControllerContext context, RequiredAttribute … Read more