J'ai aussi eu votre problème quand je migré mon site MVC à partir de Visual Studio 2008 à Visual Studio 2010.
Le principal ASPX est ci-dessous, il a un ViewData qui appelle un contrôleur de catégorie afin de remplir ViewData [ "Catégories"] avec la collection SelectList. Il y a aussi un script pour appeler un contrôleur de sous-catégorie pour remplir le second combo avec javascript. Maintenant, j'ai été capable de le réparer en ajoutant l'attribut AlloGet sur ce second contrôleur.
est ici le ASPX et javascript
<head>
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#CategoryId").change(function() {
var categoryId = $(this)[0].value;
$("#ctl00_MainContent_SubcategoryId").empty();
$("#ctl00_MainContent_SubcategoryId").append("<option value=''>-- select a category --</option>");
var url = "/Subcategory/Subcategories/" + categoryId;
$.getJSON(url, { "selectedItem": "" }, function (data) {
$.each(data, function (index, optionData) {
$("#ctl00_MainContent_SubcategoryId").append("<option value='" + optionData.SubcategoryId + "'>" + optionData.SubcategoryName + "</option>");
});
//feed our hidden html field
var selected = $("#chosenSubcategory") ? $("#chosenSubcategory").val() : '';
$("#ctl00_MainContent_SubcategoryId").val(selected);
});
}).change();
});
</script>
<body>
<% using (Html.BeginForm()) {%>
<label for="CategoryId">Category:</label></td>
<%= Html.DropDownList("CategoryId", (SelectList)ViewData["Categories"], "--categories--") %>
<%= Html.ValidationMessage("category","*") %>
<br/>
<label class="formlabel" for="SubcategoryId">Subcategory:</label><div id="subcategoryDiv"></div>
<%=Html.Hidden("chosenSubcategory", TempData["subcategory"])%>
<select id="SubcategoryId" runat="server">
</select><%= Html.ValidationMessage("subcategory", "*")%>
<input type="submit" value="Save" />
<%}%>
ici est mon contrôleur pour les sous-catégories
public class SubcategoryController : Controller
{
private MyEntities db = new MyEntities();
public int SubcategoryId { get; set; }
public int SubcategoryName { get; set; }
public JsonResult Subcategories(int? categoryId)
{
try
{
if (!categoryId.HasValue)
categoryId = Convert.ToInt32(RouteData.Values["id"]);
var subcategories = (from c in db.Subcategories.Include("Categories")
where c.Categories.CategoryId == categoryId && c.Active && !c.Deleted
&& c.Categories.Active && !c.Categories.Deleted
orderby c.SubcategoryName
select new { SubcategoryId = c.SubcategoryId, SubcategoryName = c.SubcategoryName }
);
//just added the allow get attribute
return this.Json(subcategories, JsonRequestBehavior.AllowGet);
}
catch { return this.Json(null); }
}
est une propriété "JsonRequestBehavior" ont été ajoutés que dans MVC2. Becoz J'ai essayé de chercher ceci sur mvc 1.0 et je n'ai pas pu le découvrir. – Santhosh
Oui, il a été ajouté en v2. Au moins, les docs 1.0 ici (http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult_members.aspx) ne le listent pas. – Jedidja
thnks pour l'info. – Santhosh