[英]How do I get the value of my second dropdownlist in MVC4?
I've got these two cascading dropdown lists, they populate just fine. 我有这两个级联的下拉列表,它们填充得很好。 But when I hit submit I always gett NULL as the value of the second.
但是当我点击提交时,我总是得到NULL作为第二个值。 My guess is that I have to do something with the javascript, but my skills in that department are seriously lacking.
我的猜测是我必须使用javascript做一些事情,但是我在那个部门的技能严重不足。 Please help!
请帮忙!
Code removed, whole view added instead 删除代码,改为添加整个视图
I dont even have an HtmlHelper for the second dropdown, it just looks like this in the view: 我什至没有第二个下拉列表的HtmlHelper,它在视图中看起来像这样:
Code removed, whole view added instead 删除代码,改为添加整个视图
Works just fine, I mean, it populates fine depending on what's chosen in the first dropdown. 工作正常,我的意思是,根据在第一个下拉菜单中选择的内容,它可以很好地填充。 Maybe this is the part that needs alteration?
也许这是需要改动的部分? The thing is I'm clueless.
事情是我一无所知。
EDIT: 编辑:
This is the code that reads the information from the form and submits it to the database. 这是从表单读取信息并将其提交到数据库的代码。
[HttpPost]
public ActionResult Returer(ReturerDB retur)
{
if (ModelState.IsValid)
{
db2.ReturerDB.AddObject(retur);
db2.SaveChanges();
return RedirectToAction("Returer");
}
return View("Returer");
}
EDIT2 EDIT2
The whole view code: 整个查看代码:
@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")
}
Currently you are not retrieving the additional value "bolag" in your action. 当前,您没有在操作中检索附加值“ bolag”。 I guess your model does not have property named "bolag".
我猜您的模型没有名为“ bolag”的属性。 You can either add that or you can add additional parameter to you action like so:
您可以添加它,也可以在操作中添加其他参数,如下所示:
[HttpPost]
public ActionResult Returer(ReturerDB retur, string bolag)
{
if (ModelState.IsValid)
{
db2.ReturerDB.AddObject(retur);
db2.SaveChanges();
return RedirectToAction("Returer");
}
return View("Returer");
}
After that, the selected value from the dropdown should automatically be in the bolag-parameter. 之后,从下拉列表中选择的值应自动位于bolag参数中。
Edit. 编辑。
You also need to add name-attribute to your select: 您还需要将名称属性添加到您的选择中:
<select id="bolag" name="bolag"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.