我有这两个级联下拉列表,它们填充得很好。但是当我点击提交时,我总是将NULL作为第二个值。我的猜测是,我必须用javascript做些事情,但我在该部门的技能严重不足。请帮忙!如何在MVC4中获得我的第二个下拉列表的值?
代码中删除,整个视图中添加,而不是
我不甚至有第二个下拉的的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")
}
你是如何尝试读取值,发布相关的服务器端代码。 – Esko
您可以使用Firebug/Chrome开发工具的网络选项卡中的Chrome和FF检查提交的POST数据 – Vogel612
@Esa我想我发布了您提到的内容。 –