miałem też problemu, kiedy migracji moją stronę MVC z Visual Studio 2008 do Visual Studio 2010.
Głównym aspx jest poniżej, ma ViewData który wywołuje kategorię Controller w celu wypełnienia się ViewData [ "Kategorie"] z kolekcją SelectList. Istnieje również skrypt do wywoływania kontrolera podkategorii w celu uzupełnienia drugiego combo za pomocą javascript. Teraz udało mi się to naprawić, dodając atrybut AlloGet do tego drugiego kontrolera.
Oto aspx i 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" />
<%}%>
oto mój kontroler dla podkategorii
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); }
}
Czy "JsonRequestBehavior" własność zostały dodane tylko w mvc2. Becoz Próbowałem przeszukać to na mvc 1.0 i nie mogłem się dowiedzieć. – Santhosh
Tak, została dodana w wersji 2. Przynajmniej 1.0 dokumenty tutaj (http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult_members.aspx) nie wyświetlają tego. – Jedidja
thnks dla informacji. – Santhosh