So I was playing around with my ASP.NET MVC 4 solution. Every thing worked fine kept adding things but something odd started happening.
One of my Models properties was null, even though I had items in the Json passed to it.
This was the javascript object/json passed it it:
var obj = {
"plc": "False",
"al": ["386", "710"],
"pl": ["9530", "211", "783"]
};
I was using a Custom Model binder ... thought that might be the issue so I turned it off.
Tried using the JavaScriptSerializer from .NET to see it that worked:
var reader = new StreamReader(Request.InputStream);
Request.InputStream.Position = 0;
var readToEnd = reader.ReadToEnd();
var javaScript = new JavaScriptSerializer();
var searchFarmOptions = javaScript.Deserialize<Test>(readToEnd);
Got all the properties set ... WOOT.
So I tried a clean ASP.NET MVC 4 solution. To reproduce the bug.
This is from the Index.cshtml view
#{
ViewBag.Title = "title";
}
<h1>Title</h1>
Testing ...
<script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script>
$(function() {
var obj = {
"1pllort": "False",
"1plc": "true",
"al": ["386", "710"],
"pl": ["9530", "211", "783"]
};
var options = {
"contentType": "application/json; charset=UTF-8",
"type": "POST",
"data" : JSON.stringify(obj)
};
$.ajax("/Home/TestPost", options).done(function(data) {
console.log(data);
});
});
</script>
This is my HomeController
using System.Collections.Generic;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View("Index");
}
[HttpPost]
public ActionResult TestPost(Test model)
{
return Json(model);
}
}
public class Test
{
public List<int> PL { get; set; }
public List<int> AL { get; set; }
public bool PLC { get; set; }
public bool ALC { get; set; }
}
}
Yes, the bug is still there.
Whenever I have a property starting with "pl" as my list name is .. the "pl" list is null.
Also, it could be any name starting with "pl" ... like "plchecked"
If I rename the "plc" to "cpl" its working.
So what is going on here ... is there any naming restrictions in the model binder? What am I missing here?
Update 1
Work
PL server side now have the right values, etc. not null, but the list of numbers.
var obj = {
"pl": ["9530", "211", "783"],
"1plc": "false",
"pl-some-odd-value": "false",
"al": ["386", "710"],
"alc": "false"
};
Don't work
PL server side now have null value.
var obj = {
"pl": ["9530", "211", "783"],
"al": ["386", "710"],
"alc": "false",
"pl-odd-value": "false"
};
Work
PL has the 3 values som the json object string ...
var obj = {
"pl": ["9530", "211", "783"],
"al": ["386", "710"],
"alc": "false",
"odd-value-pl": "false"
};
Install ImranB.ModelBindingFix nuget package. I think you are hitting a bug which I have discussed in this blog post.
There is definitely a bug. I just spent hours debugging the MVC 4 source code for the same problem. The primary root cause it that you have a collection whose name is the beginning of another property name. In your example: pl and pl-odd-value.
If you play around some more, you may find that sometimes it works properly. The reason for it seeming to be random is that the model binder performs a binary search to locate the value to bind. The binary search uses a "divide and conquer" approach to finding the property in a sorted list of the properties that were posted. If the search tests the p1-odd-value in its divide and conquer, the problem occurs. If the search skips this value, no problem.
This problem is most repeatable when you simply have the pl collection with 1 element and the pl-odd-value property in the object.
Here is a bit more info related to this. There is a sortedList of all of the values posted back. In the case of your failed test, the sorted list looks like this:
alc, "false",
al[0], "386"
al[1], "710"
pl-odd-value, "false"
pl[0], "9530"
pl[1], "211"
pl[2], "783"
Notice that the sort order uses StringComparer.OrdinalIgnoreCase, which places the square bracket items AFTER the text-only item. When MVC is performing its binary search for the al or pl collection, it is searching for al or pl, NOT al[ or pl[. By searching for the collection name without a square bracket, al < alc, not greater than alc. Problem is that we want to look after alc or pl-odd-value to get to the collection.
The workaround is to make sure that none of your property names start with the name of any of your collection property names. Rename your al collection to maybe al_collection and your pl collection to maybe pl_collection.
Great answer!
I had the same problem today and I spent almost 3 hours trying to figure out what the issue was when suddenly I realized that properties with the same prefix has something weird
More info here:
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=mvc+4+model+binder+bug
ASP.NET MVC 4 - ModelBinder bug
https://gist.github.com/adamchester/5606734
Related
In my MVC 5 app I need to be able to dynamically construct a list of fully qualified external URL hyperlinks, alone with some additional data, which will come from the Model passed in. I figure - I will need to construct my anchor tags something like this:
{{linkDisplayName}}
with AngularJS this would be natural, but, I have no idea how this is done in MVC.
Is there a templating library that can be used for this?
1) Create a model to Hold the Links
public class LinkObject
{
public string Link { get; set; }
public string Description { get; set; }
}
2) In your Action you can use ViewBag, ViewData or even pass the list inside you Model. I will show you how to do using ViewBag
public ActionResult MyDynamicView()
{
//Other stuff and code here
ViewBag.LinkList = new List<LinkObject>()
{
new LinkObject{ Link ="http://mylink1.com", Description = "Link 1"},
new LinkObject{ Link ="http://mylink2.com", Description = "Link 2"},
new LinkObject{ Link ="http://mylink3.com", Description = "Link 3"}
};
return View(/*pass the model if you have one*/);
}
3) In the View, just use a loop:
#foreach (var item in (List<LinkObject>)ViewBag.LinkList)
{
#item.Description
}
Just create a manual one for that, no need to do it from a template. For example, in javascript
function groupAnchor(url,display){
var a = document.createElement("a");
a.href = url;
a.className = "list-group-item";
a.target = "_blank";
a.innerHTML = display;
return a;
}
And then use that function to modify your html structure
<div id="anchors"></div>
<script>
document.getElementById("anchors").appendChild(groupAnchor("http://google.com","Google"));
</script>
Your approach to modification will more than likely be more advanced than this, but it demonstrates the concept. If you need these values to come from server side then you could always iterate over a set using #foreach() and issue either the whole html or script calls there -- or, pass the set from the server in as json and then use that in a function which is set up to manage a list of anchors.
To expand on this, it is important to avoid sending html to the view from a razor iteration. The reason being that html constructed by razor will increase the size of the page load, and if this is done in a list it can be a significant increase.
In your action, construct the list of links and then serialize them so they can be passed to the view
public ActionResult ViewWithLinks()
{
var vm = new ViewModel();
vm.Links = Json(LinkSource.ToList()).Data;
//or for a very simple test for proof of concept
var Numbers = Json(Enumerable.Range(0,100).ToList()).Data;
ViewData["numbers"] = Numbers ;
return View(vm);
}
where all you need is an object to hold the links in your view model
public class ViewModel
{
public ICollection<Link> Links { get; set; }
}
public class Link
{
public string text { get; set; }
public string href { get; set; }
}
and then in your view you can consume this json object
var allLinks = #Html.Raw(Json.Encode(Model.Links));
var numbersList = #Html.Raw(Json.Encode(ViewData["linkTest"]));//simple example
Now you can return to the above function in order to place it on the page by working with the array of link objects.
var $holder = $("<div>");
for(var i = 0; i < allLinks.length; i++){
$holder.append(groupAnchor(allLinks[i].href,allLinks[i].text));
}
$("#linkArea").append($holder);
The benefit is that all of this javascript can be cached for your page. It is loaded once and is capable of handling large amounts of links without having to worry about sending excessive html to the client.
So here's the deal, i want to be able to export any Enumerable of items to excel:
Here's an ActionMethod in some Area of my app that constructs an "ExportToExcel" model, then Redirects it to an Action Method in another controller and another are which does all the formatting-to-excel work:
public ActionResult ExportCustomListToExcel()
{
var exportModel = new ExportToExcelModel();
//Here I fill up the model with a dataTable / other file info like
//exportModel.Items = blah blah..
return RedirectToAction("ExportToExcel", "Shared", new { model = exportModel, testString = "test", area = "Shared" });
}
And here's my Shared ExportToExcel ActionMethod:
public ActionResult ExportToExcel(ExportToExcelModel model, string testString)
{
//PROBLEM IS RIGHT HERE!
// where testString == "test"
// but model == null :(
//Ommited unrelated code
}
My ExportToExcel actionMethod gets hit, but somewhere along the way my ExportToExcelModel gets lost :(
Note: It succeeds on passing strings like "testString" so is there somwthing wrong with my model?
Just in case, the ExportToExcelModel is:
public class ExportToExcelModel
{
public ExportToExcelModel() {}
public ExportToExcelModel(string fileName, ItemType itemType, IEnumerable<ExportableToExcelItem> items)
{
this.FileName = fileName;
this.ItemType = ItemType;
this.Items = items;
}
public string FileName { get; set; }
public ItemType ItemType { get; set; }
public IEnumerable<ExportableToExcelItem> Items { get; set; }
}
Thanks in advance!
First time i've ever needed to actually ask a question here since every other question i've ever had i've found already answered here :)
EDIT: Posting FormCollection results:
http://imageshack.us/photo/my-images/861/sinttulonsa.png
Sorry, newbies cant post pics :(
The reason is that a RedirectToAction result will launch a GET request and your parameters will have to be passed along through the querystring. Obviously there is a limit to the amount of characters a url can consist of.
Seems to me that you should do the conversion to Excel in a class instead of behind another Action.
So CustomExportAction1 and CustomExportAction2 both call
return File(ExcelExporter.ExportExcel(dataToExport));
or something similar.
try to switch your ExportToExcel signature to
public ActionResult ExportToExcel(FormCollection data)
{
var model = new ExportToExcelModel();
try
{
UpdateModel(model, data)
}
catch(UpdateModelException ex)
{
}
}
look at what's in the FormCollection (that might help), and also see if UpdateModel is throwing an exception, because this is what is happening behind the seen when you make your action method take in a model instead of a FormCollection.
Hope that help you track it down
UPDATE:
You might have to do it using TempData, read this, supposedly you can't do this out of the box with ASP.NET MVC!!
I have an enum for one of the properties of my view-model. I want to display a drop-down list that contains all the values of the enum. I can get this to work with the following code.
What I'm wondering is whether there is a simple way to convert from an enum to an IEnumerable? I can do it manually as in the following example, but when I add a new enum value the code breaks. I imagine that I can do it via reflection as per this example, but but are there other ways to do this?
public enum Currencies
{
CAD, USD, EUR
}
public ViewModel
{
[Required]
public Currencies SelectedCurrency {get; set;}
public SelectList Currencies
{
List<Currencies> c = new List<Currencies>();
c.Add(Currencies.CAD);
c.Add(Currencies.USD);
c.Add(Currencies.EUR);
return new SelectList(c);
}
}
I'm using a helper that i found here to populate my SelectLists with a generic enum type, i did a little modification to add the selected value though, here's how it looks like :
public static SelectList ToSelectList<T>(this T enumeration, string selected)
{
var source = Enum.GetValues(typeof(T));
var items = new Dictionary<object, string>();
var displayAttributeType = typeof(DisplayAttribute);
foreach (var value in source)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DisplayAttribute attrs = (DisplayAttribute)field.
GetCustomAttributes(displayAttributeType, false).FirstOrDefault()
items.Add(value, attrs != null ? attrs.GetName() : value.ToString());
}
return new SelectList(items, "Key", "Value", selected);
}
The nice thing about it is that it reads the DisplayAttribute as the title rather than the enum name. (if your enums contain spaces or you need localization then it makes your life much easier)
So you will need to add the Display attirubete to your enums like this :
public enum User_Status
{
[Display(Name = "Waiting Activation")]
Pending, // User Account Is Pending. Can Login / Can't participate
[Display(Name = "Activated" )]
Active, // User Account Is Active. Can Logon
[Display(Name = "Disabled" )]
Disabled, // User Account Is Diabled. Can't Login
}
and this is how you use them in your views.
<%: Html.DropDownList("ChangeStatus" , ListExtensions.ToSelectList(Model.statusType, user.Status))%>
Model.statusType is just an enum object of type User_Status.
That's it , no more SelectLists in your ViewModels. In my example I'm refrencing an enum in my ViewModel but you can Refrence the enum type directly in your view though. I'm just doing it to make everything clean and nice.
Hope that was helpful.
Look at Enum.GetNames(typeof(Currencies))
I am very late on this one but I just found a really cool way to do this with one line of code, if you are happy to add the Unconstrained Melody NuGet package (a nice, small library from Jon Skeet).
This solution is better because:
It ensures (with generic type constraints) that the value really is an enum value (due to Unconstrained Melody)
It avoids unnecessary boxing (due to Unconstrained Melody)
It caches all the descriptions to avoid using reflection on every call (due to Unconstrained Melody)
It is less code than the other solutions!
So, here are the steps to get this working:
In Package Manager Console, "Install-Package UnconstrainedMelody"
Add a property on your model like so:
//Replace "YourEnum" with the type of your enum
public IEnumerable<SelectListItem> AllItems
{
get
{
return Enums.GetValues<YourEnum>().Select(enumValue => new SelectListItem { Value = enumValue.ToString(), Text = enumValue.GetDescription() });
}
}
Now that you have the List of SelectListItem exposed on your model, you can use the #Html.DropDownList or #Html.DropDownListFor using this property as the source.
So many good answers - I thought I'sd add my solution - I am building the SelectList in the view (and not in the Controller):
In my c#:
namespace ControlChart.Models
//My Enum
public enum FilterType {
[Display(Name = "Reportable")]
Reportable = 0,
[Display(Name = "Non-Reportable")]
NonReportable,
[Display(Name = "All")]
All };
//My model:
public class ChartModel {
[DisplayName("Filter")]
public FilterType Filter { get; set; }
}
In my cshtml:
#using System.ComponentModel.DataAnnotations
#using ControlChart.Models
#model ChartMode
#*..........*#
#Html.DropDownListFor(x => x.Filter,
from v in (ControlChart.Models.FilterType[])(Enum.GetValues(typeof(ControlChart.Models.FilterType)))
select new SelectListItem() {
Text = ((DisplayAttribute)(typeof(FilterType).GetField(v.ToString()).GetCustomAttributes(typeof(DisplayAttribute), false).First())).Name,
Value = v.ToString(),
Selected = v == Model.Filter
})
HTH
Maybe is too late, but i think it could be useful for people with the same problem.
I've found here that now with MVC 5 it's included an EnumDropDownListFor html helper that makes for no longer necesary the use of custom helpers or other workarounds.
In this particular case, just add this:
#Html.EnumDropDownListFor(x => x.SelectedCurrency)
and that's all!
You can also translate or change the displayed text via data annotations and resources files:
Add the following data annotations to your enum:
public enum Currencies
{
[Display(Name="Currencies_CAD", ResourceType=typeof(Resources.Enums)]
CAD,
[Display(Name="Currencies_USD", ResourceType=typeof(Resources.Enums)]
USD,
[Display(Name="Currencies_EUR", ResourceType=typeof(Resources.Enums)]
EUR
}
Create the corresponding resources file.
i am kind of stuck with this code:
[AcceptVerbs(HttpVerbs.Post), Authorize]
public ActionResult Edit(FormCollection collection, [Bind(Exclude="Id,Latitude,Longitude")]Store bindStore, string Latitude, string Longitude)
{
Store st = storeModel.GetStore(Session["user_id"].ToString());
bool modelUpdate = TryUpdateModel(st, new string[] { "Storename", "Address1", "Address2", "Zipcode", "City", "Phone1", "Phone2", "Store_email", "Store_homepage", "Pm_ec", "Pm_visa", "Pm_master", "Appointment_requirement", "Short_user_store_description" });
if (ModelState.IsValid)
{
storeModel.Save();
return RedirectToAction("ImageUpload");
}
else
return Edit();
}
My problem is, that i don't know whats the right way to do the ModelBinding, when i need these conditions to match:
The "Store" object has several fields ( not only Id ) that should NOT be updated during this update ( had problems that the "TryUpdateModel" NULL-ifies all these fields, so i added the "includeProperties" value with the cleartext of all the params.. a solution i don't like anyway)
Getting the store id from session as i don't want it to be in the HTML
Making InputValidation through the DataAnnotationModel ( see below )
[MetadataType(typeof(StoreMetaData))]
public partial class Store
{
}
public class StoreMetaData
{
[Required]
[StringLength(5)]
public object Storename { get; set; }
}
Question: Is this the right approach or is there a standard/nicer solution for this kind of problem?
You don't need formcollection in parameters for start.
And no, updatemodel will try to update all fields of model if no white or black list defined.
So either that or create your own function to update your model objects.
Maybe somethig generic. that will reflection enumerate properties of supplied update object and apply those that are not null to object being updated.
My guess, from looking at the code you posted, is that you're trying to make it so that the Edit view will only allow certain fields to be edited, but 'pass through' the ones you don't want changed. In that case, you can modify your Edit view to remove those fields from being edited, but still send them over in the form using:
<%= Html.Hidden("field") %>
Let me know if this is what you intended.
Your way is fine IMHO.
There are other options but none of them provide a compelling benefit over your technique.
The only thing I would do differently is to model bind to custom ViewModel with the exact fields you need and then do the rightly-lefty code to assign just those values:
public ActionResult Edit( SlimStoreViewmodel editStoreModel, string Latitude, string Longitude)
{
Store st = storeModel.GetStore(Session["user_id"].ToString());
if (ModelState.IsValid)
{
st.Thing = editStoreModel.Thing;
st.Thing2 = editStoreModel.Thing2;
st.Widget = editStoreMOdel.Widget;
storeModel.Save();
return RedirectToAction("ImageUpload");
}
else
return Edit();
}
This gets rid of long magic string dense things like this:
TryUpdateModel(st, new string[] { "Storename", "Address1", "Address2", "Zipcode", "City", "Phone1", "Phone2", "Store_email", "Store_homepage", "Pm_ec", "Pm_visa", "Pm_master", "Appointment_requirement", "Short_user_store_description" });
Forgive me if this has been asked before; I couldn't find anything close after a few searches:
I'm trying to write an ActionFilter in MVC that will "intercept" an IQueryable and nullify all the parent-child relationships at runtime. I'm doing this because Linq does not serialize objects properly if they have parent-child relationships (it throws a circular reference error because the parent refers to the child, which refers back to the parent and so on), and I need the object serialized to Json for an Ajax call. I have tried marking the child relationship in the DBML file with a privacy status of internal, and while this fixes the serialization problem, it also hides the child members from the view engine when the page renders, causing another error to be thrown. So, by fixing one problem, I cause another.
The only thing that fixes both problems is to manually set the child members to null just before returning the serialization, but I'm trying to avoid doing that because it's cumbersome, not reusable, etc. I'd rather use an ActionFilter to inspect the IQueryable that is being serialized and nullify any members with a Type of EntitySet (how Foreign Keys/Associations are represented). However, I don't have much experience with Reflection and can't find any examples that illustrate how to do something like this. So... is this possible with Reflection? Is there a better way to accomplish the same thing? I'll post the relevant code tomorrow when I'm back at my work computer.
Thanks,
Daniel
As promised, the code:
[GridAction]
public ActionResult _GetGrid()
{
IQueryable<Form> result = formRepository.GetAll();
foreach (Form f in result)
{
f.LineItems = null;
f.Notes = null;
}
return View(new GridModel<Form> { Data = result });
}
An added wrinkle is that I'm using the new Telerik MVC Extensions, so I'm not actually serializing the Json myself -- I'm just returning the IQueryable in an IGridModel, and the action filter [GridAction] does the rest.
So, just in case anyone's curious, here's how I finally solved this problem: I modified Damien Guard's T4 template to include the attribute [ScriptIgnore] above entities of type Association. This lets the JSON serializer know to not bother serializing these, thus preventing the circular reference problem I was getting. The generated code ends up looking like this:
private EntitySet<LineItem> _LineItems;
[ScriptIgnore]
[Association(Name=#"Form_LineItem", Storage=#"_LineItems", ThisKey=#"Id", OtherKey=#"FormId")]
public EntitySet<LineItem> LineItems
{
get {
return _LineItems;
}
set {
_LineItems.Assign(value);
}
}
This fixes the serialization problem I was having without disabling the use of child tables through LINQ. The grid action on the controller ends up looking like this:
[GridAction]
public ActionResult _GetGrid()
{
return View(new GridModel<Form> { Data = formRepository.GetAll() });
}
There are two options, one is to ignore those properties during serialization using [XmlIgnore]. The other one is to nullify the properties using reflection.
Ignore in serialization, simple usage sample that shows how to use default value in serialization:
[Serializable]
public class MyClass
{
[XmlIgnore]
public int IgnoredVal { get; set; }
public int Val { get; set; }
}
public void UsageSample()
{
var xmlSerializer = new XmlSerializer(typeof(MyClass));
var memoryStream = new MemoryStream();
var toSerialize = new MyClass { IgnoredVal = 1, Val = 2 };
xmlSerializer.Serialize(memoryStream, toSerialize);
memoryStream.Position = 0;
var deserialize = (MyClass)xmlSerializer.Deserialize(memoryStream);
Assert.AreEqual(0, deserialize.IgnoredVal);
Assert.AreEqual(2, deserialize.Val);
}
Nullify with reflection, code sample:
public void NullifyEntitySetProperties(object obj)
{
var entitySetProperties = obj.GetType().GetProperties()
.Where(property => property.PropertyType == typeof(EntitySet));
foreach (var property in entitySetProperties)
{
property.SetValue(obj, null, null);
}
}
In my opinion, if the first option can be done used in your code it's better. This option is more direct and economic.