我很新的JavaScript,jQuery和AJAX。所以我有一個模型:如何從asp.net Mvc中的層疊列表中存儲文本值元素?
namespace hiophop.Models
{
public class CarMake
{
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
}
}
}
我創建的類的列表,並加入到他們在我的控制,同時通過JSON的觀點:
namespace hiophop.Controllers
{
public class CarController : Controller
{
List<Category> lstCat = new List<Category>()
{
new Category() { CategoryID = 1, CategoryName = "Dairy" },
new Category() { CategoryID = 2, CategoryName = "Meat" },
new Category() { CategoryID = 3, CategoryName = "Vegetable" }
};
List<Product> lstProd = new List<Product>()
{
new Product() { ProductID = 1, ProductName = "Cheese", CategoryID = 1 },
new Product() { ProductID = 2, ProductName = "Milk", CategoryID = 1 },
new Product() { ProductID = 3, ProductName = "Yogurt", CategoryID = 1 },
new Product() { ProductID = 4, ProductName = "Beef", CategoryID = 2 },
new Product() { ProductID = 5, ProductName = "Lamb", CategoryID = 2 },
new Product() { ProductID = 6, ProductName = "Pork", CategoryID = 2 },
new Product() { ProductID = 7, ProductName = "Broccoli", CategoryID = 3 },
new Product() { ProductID = 8, ProductName = "Cabbage", CategoryID = 3 },
new Product() { ProductID = 9, ProductName = "Pepper", CategoryID = 3 }
};
public ActionResult GetCategories()
{
return Json(lstCat, JsonRequestBehavior.AllowGet);
}
public ActionResult GetProducts(int intCatID)
{
var products = lstProd.Where(p => p.CategoryID == intCatID);
return Json(products, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string c ,string p)
{
ViewBag.x ="Product:"+ p;
ViewBag.y = "category" + c;
return View();
}
}
}
這裏是被它被混淆我如何檢索所選列表框的文本值我只能檢索CategoryId的Int索引。我希望CategoryName ProductName都是字符串。這裏是我的觀點:viewbags.x和y只返回Ids。我已經嘗試了幾件事,但我陷入了困境中,我做錯了什麼?我留下了一些評論,讓你看看我在嘗試什麼。
@model hiophop.Models.CarMake
@{
ViewBag.Title = "CarView";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@*<h2>CarView</h2>*@
@ViewBag.y
@ViewBag.x
@using (Html.BeginForm("Index", "Car", FormMethod.Post))
{
<div>
<label for="category">Category</label>
<select id="category" name="c" class="form-control"></select>
<label for="product">Product</label>
<select id="product" name="p" class="form-control"></select>
<div id="result"></div>
<input type="submit" id="Button1" class="btn btn-default" />
</div>
}
@section scripts {
<script type="text/javascript">
$(document).ready(function() {
// Get a list of categories and a list of products of the first category.
$.getJSON('/Car/GetCategories', null, function (data) {
$.each(data, function() {
$('#category').append('<option value=' +
this.CategoryID + '>' + this.CategoryName + '</option>');
});
$.getJSON('/Car/GetProducts', { intCatID: $('#category').val() }, function (data) {
$.each(data, function() {
$('#product').append('<option value=' +
this.ProductID + '>' + this.ProductName + '</option>');
});
}).fail(function (jqXHR, textStatus, errorThrown) {
alert('Error getting products!');
});
}).fail(function (jqXHR, textStatus, errorThrown) {
alert('Error getting categories!');
});
// Dropdown list change event.
$('#category').change(function() {
$('#product option').remove();
$.getJSON('/Car/GetProducts', { intCatID: $('#category').val() }, function (data) {
$.each(data, function() {
$('#product').append('<option value=' +
this.ProductID + '>' + this.ProductName + '</option>');
});
}).fail(function (jqXHR, textStatus, errorThrown) {
alert('Error getting products!');
});
});
});
//var result = $('#result');
//$(document).ready(function() {
$("#Button").click(function() {
var request = $('#category option:selected').text() + "," + $('#product option:selected').text();
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: '/Car/Index',
//data: "{'Category':'" + document.getElementById('#category') + "','food':'" + document.getElementById('#product') + " '}",
// async: false,
// success: function (response) {
// $('#category').val('');
// $('#product').val('');
// alert("record has been saved in database");
// },
// error: function() {
// console.log('there is some error');
// }
data: { c: request },
dataType: 'json',
success: function (data) {
result.html('#category: ' + data.CategoryName + '' + '#product' + data.ProductName)
}
});
});
//});
</script>
}
你的代碼真的沒有意義,它不清楚你想做什麼。如果你想回發每個選擇的選項的文本,那麼它的'data:{c:$('#category option:selected')。text(),p:$('#product option:selected')。text ); }'。但是你指定你在ajax調用中返回'json',所以你的'Index()'post方法會拋出一個異常,因爲你的返回html –
也建議你研究[DotNetFiddle]中的代碼(https:// dotnetfiddle。 net/1bPZym)瞭解如何實現級聯下拉列表 –