【发布时间】:2014-01-24 07:51:11
【问题描述】:
我有这两个级联下拉列表,它们填充得很好。但是当我点击提交时,我总是得到 NULL 作为第二个的值。我的猜测是我必须对 javascript 做点什么,但我在那个部门的技能严重缺乏。请帮忙!
删除代码,添加整个视图
我什至没有第二个下拉菜单的 HtmlHelper,它在视图中看起来像这样:
删除代码,添加整个视图
工作得很好,我的意思是,它填充得很好,具体取决于第一个下拉列表中选择的内容。也许这是需要改变的部分?问题是我一无所知。
编辑:
这是从表单中读取信息并将其提交到数据库的代码。
[HttpPost]
public ActionResult Returer(ReturerDB retur)
{
if (ModelState.IsValid)
{
db2.ReturerDB.AddObject(retur);
db2.SaveChanges();
return RedirectToAction("Returer");
}
return View("Returer");
}
EDIT2
整个视图代码:
@model Fakturabolag.Models.ReturerDB
@{
ViewBag.Title = "Returer";
}
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#bolag").prop("disabled", true);
$("#Kund").change(function () {
if ($("#Kund").val() != "- Välj bolag -") {
var options = {};
options.url = "/companies/getbolag";
options.type = "POST";
options.data = JSON.stringify({ country: $("#Kund").val() });
options.dataType = "json";
options.contentType = "application/json";
options.success = function (bolag) {
$("#bolag").empty();
for (var i = 0; i < bolag.length; i++) {
$("#bolag").append("<option>" + bolag[i] + "</option>");
}
$("#bolag").prop("disabled", false);
};
options.error = function () { alert("Fel vid bolagshämtning!"); };
$.ajax(options);
}
else {
$("#bolag").empty();
$("#bolag").prop("disabled", true);
}
});
});
</script>
<h2>Returer</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Returer</legend>
<br /><br />
<div class="editor-label">
<b>Kund</b>
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Kund, ViewData["kundLista"] as SelectList)
@Html.ValidationMessageFor(model => model.Kund)
</div>
<div class="editor-label">
<b>Bolag</b>
</div>
<select id="bolag"></select>
<div class="editor-label">
<b>Pris</b>
</div>
<div class="editor-field">
@Html.TextBox("pris", null, new { style = "width: 150px" })
@Html.ValidationMessageFor(model => model.Pris)
</div>
<div class="editor-label">
<b>Datum</b>
</div>
<div class="editor-field">
@Html.TextBox("datum", ViewData["datum"] as String, new { style = "width: 150px" })
@Html.ValidationMessageFor(model => model.Datum)
</div>
<p>
<input type="submit" value="Lägg till" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
【问题讨论】:
-
你是如何读取值的,贴出相关的服务器端代码。
-
您可以在 Firebug / Chrome Dev Tools 的 Network 标签中使用 Chrome 和 FF 检查提交的 POST 数据
-
@Esa 我想我发布了你提到的内容。
-
但是你是打ajax调用还是正常提交?您使用默认模型绑定器吗?因为我想知道你是如何传递它的——首先没有分配“名称”属性,其次它不是命名约定的一部分,当你使用强类型视图和 Html 扩展方法时,默认情况下它存在,这使默认模型绑定器能够适当地解释表单集合元素。也许您可以粘贴整个视图代码?
-
应该是正常提交。我是 MVC 的新手,从未使用过它,但在工作中得到了几个 MVC 项目,我必须继续开发。我边走边学,没有官方知识。所以请耐心等待我尝试回答您的问题:)
标签: c# javascript asp.net-mvc-4 cascadingdropdown