2014-01-24 64 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感謝您的幫助! –

相關問題