2014-01-24 30 views
1

我有这两个级联下拉列表,它们填充得很好。但是当我点击提交时,我总是将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") 
} 
+1

你是如何尝试读取值,发布相关的服务器端代码。 – Esko

+0

您可以使用Firebug/Chrome开发工具的网络选项卡中的Chrome和FF检查提交的POST数据 – Vogel612

+0

@Esa我想我发布了您提到的内容。 –

回答

2

目前你是不是在你的行动时获得额外的价值 “bolag”。我猜你的模型没有名为“bolag”的属性。您可以添加,也可以像这样给你操作添加额外的参数:

[HttpPost] 
    public ActionResult Returer(ReturerDB retur, string bolag) 
    { 
     if (ModelState.IsValid) 
     { 
      db2.ReturerDB.AddObject(retur); 
      db2.SaveChanges(); 

      return RedirectToAction("Returer"); 
     } 

     return View("Returer"); 
    } 

之后,从下拉列表中选择的值应自动在bolag参数。

编辑。

您还需要名称属性添加到您的选择:

<select id="bolag" name="bolag"/> 
+0

该模型包含“bolag”属性。我只是将名称属性添加到我的选择,它的工作!我的一个真正的无意义的错误,但事情发生:P感谢您的帮助! –

相关问题