我simpel测试型号:无法通过选择价值DropDownListFor到JavaScript函数
public class MovieModel {
public string SelectedCategorieID { get; set; }
public List<CategorieModel> Categories { get; set; }
public MovieModel() {
this.SelectedCategorieID = "0";
this.Categories = new List<CategorieModel>() {new CategorieModel {ID = 1,
Name = "Drama"},
new CategorieModel {ID = 2,
Name = "Scifi"}};
}
}
public class CategorieModel {
public int ID { get; set; }
public string Name { get; set; }
}
我家的控制器操作指数:
public ActionResult Index() {
Models.MovieModel mm = new Models.MovieModel();
return View(mm);
}
我的强类型查看:
@model MvcDropDownList.Models.MovieModel
@{
ViewBag.Title = "Home Page";
}
<script type="text/javascript">
function categoryChosen(selectedCatID) {
// debugger;
var url = "Home/CategoryChosen?SelectedCategorieID=" + selectedCatID;
$.post(url, function (data) {
$("#minicart").html(data);
});
}
</script>
@using (Html.BeginForm("CategoryChosen", "Home", FormMethod.Get)) {
<fieldset>
Movie Type
@Html.DropDownListFor(m => m.SelectedCategorieID, new SelectList(Model.Categories, "ID", "Name", Model.SelectedCategorieID), "---Select categorie---")
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
<input type="button" value="Minicart test" onclick="categoryChosen('@Model.SelectedCategorieID');" />
<div id="minicart">
@Html.Partial("Information")
</div>
请忽略第一个输入,因为我正在使用第二个输入'Minicart测试'(HTML.Beginform在那里稍后学习别的东西)。迷你车的东西来自另一个教程,我很抱歉。不要让它让你分心。
当按钮被点击categoryChosen jQuery是调用,它调用的动作:
[AcceptVerbs("POST")]
public ActionResult CategoryChosen(string SelectedCategorieID) {
ViewBag.messageString = SelectedCategorieID;
return PartialView("Information");
}
局部视图的信息是这样的:
@{
ViewBag.Title = "Information";
}
<h2>Information</h2>
<h2>You selected: @ViewBag.messageString</h2>
我的问题是,为什么是Model.SelectCategorieID为零(Model.SelectCategorieID = 0),即使我更改了下拉列表中的值?我究竟做错了什么?非常感谢您提前回答。如果您需要任何信息或任何不清楚的地方,请告诉我。
达林,非常感谢你的广泛答复*需要一口气*。好的,首先,答案的确是从DropDownList本身获取值。所以我现在在我的javascript中做的是:var selCatID = $(“#Cats”)。val(); (我给了下拉ID Card of course)。非常感谢你的回答。 – 2013-03-05 13:55:50
但是你的回答给了我更多的问题,因为我在web开发方面很新颖。为什么值硬编码,如果我说:@ Model.SelectedCategorieID?我认为值发生变化后存储在这个属性?什么是不显眼的,在单独的文件中设置JavaScript? – 2013-03-05 14:06:06
什么是.data('url')?那是为了提取drowpdownlistbox所在的网址吗?我不明白这将如何称为行动“类别选择”... – 2013-03-05 14:08:11