2015-07-10 78 views
1

我有一個使用Kendo控件的MVC項目。其中一個視圖是一個下拉框和文本框。兩者最初都是從模型中獲得它們的價值。如何在用戶從下拉列表中選擇一個項目時更改模型(以及文本框)?在MVC項目中,如何在下拉列表更改值時更新模型?

例如,該模型是在控制器設置項的下拉框的原始值填充的基礎上的「常規」和文本框是基於對「小工具」的項目。當用戶選擇從下拉「特殊」,控制器將查詢數據庫以獲取基於「特殊」的數據,發現文本框的新的價值應該說是「裝飾物」,加上「裝飾物的模型和變化文本框 「裝飾物」。

查看

@model GPC.Models.ModelInstrumentListingDetail 
@using (Html.BeginForm("InstrumentListingDetailClick", "Home", FormMethod.Post, new { id = "InstrumentListingDetailForm" })) 
{ 
<div id="divInstrumentListingDetailHeader" class="detailDivs"> 
    <table> 
     <tr> 
     <tr> 
      <td style="text-align: right;" class="dropdowns"> 
       <label>Category:</label> 
      </td> 
     </tr> 
    </table> 
</div> // divInstrumentListingDetailHeader 

<div id="divInstrumentListingDetailBody" class="detailDivs details"> 
    <table class="details"> 
     @*Field 1*@ 
     <tr> 
      <td style="text-align: right;"> 
       @Html.DisplayFor(m => m.Label1) 
      </td> 
      <td width="2px;">&nbsp;</td> 
      <td class="dropdowns"> 
       @Html.TextBoxFor(m => m.Field1, new { @class = "details" }) 
      </td> 
     </tr> 
    </table> 
</div> // divInstrumentListingDetailBody 
} 

<script> 
function onChange_ddInstrumentCategory(arg) { 
    var categoryID = $(arg).find('option:selected').val(); 

    // Update model based on the category ID 

} 
</script> 

控制器

public ActionResult InstrumentListingEdit(TblInstrumentTag model) 
{ 
    TblInstrumentTag currentInstrumentTag = data.GetInstrumentTagByID(model.InstrumentTagID); 

    // Fill Category drop down 
    List<TblInstrumentFormCategory> categories = data.GetAllCategories(); 

    // Create model 
    ModelInstrumentListingDetail detailModel = new ModelInstrumentListingDetail 
    { 
     InstrumentTagID = currentInstrumentTag.InstrumentTagID, 
     InstrumentCategory = categories.FirstOrDefault().InstrumentFormCategoryID, 
     Field1 = currentInstrumentTag.FormCategory1Value1, 
     Label1 = categories.FirstOrDefault().Label1 + ":", 
     ieInstrumentCategories = new SelectList(categories, "InstrumentFormCategoryID", "InstrumentFormCategoryName") 
    }; 

    return View("InstrumentListingEdit", detailModel); 
} 

型號

public class ModelInstrumentListingDetail 
{ 
    // Drop down ID's 
    public int InstrumentTagID { get; set; } 
    public int InstrumentCategory { get; set; } 

    // Detail fields 
    public string Field1 { get; set; } 

    // Detail labels 
    public string Label1 { get; set; } 

    // Drop downs for add/edit page 
    public IEnumerable<SelectListItem> ieInstrumentCategories { get; set; } 
} 

我想什麼是JavaScript的去像下面這段代碼來更新文本框。我寧願不發佈整個頁面。我不希望屏幕「閃爍」;我只想讓用戶從下拉菜單中選擇一個項目,然後爲了更改文本框的值。

需要從jQuery的得到這樣的事情沒有提交表單:

public ActionResult UpdateModel(TblInstrumentTag model, int newCatgoryID) 
{ 
TblInstrumentTag currentInstrumentTag = data.GetInstrumentTagByID(model.InstrumentTagID); 

// Fill Category drop down 
List<TblInstrumentFormCategory> categories = data.GetAllCategories(); 

// Create model 
ModelInstrumentListingDetail detailModel = new ModelInstrumentListingDetail 
{ 
    InstrumentTagID = currentInstrumentTag.InstrumentTagID, 
    InstrumentCategory = categories.FirstOrDefault().InstrumentFormCategoryID, 
    Field1 = currentInstrumentTag.FormCategory2Value1, // <- Value of Field 1 has changed 
    Label1 = categories.FirstOrDefault().Label1 + ":", 
    ieInstrumentCategories = new SelectList(categories, "InstrumentFormCategoryID", "InstrumentFormCategoryName") 
}; 

return View("InstrumentListingEdit", detailModel); 
} 
+0

感謝您的答覆,但是這不是我做什麼都沒有。下拉列表中沒有我想要放入文本框的值,並且客戶端上沒有任何值具有該值。我想借此從下拉列表(客戶端)的值,查詢控制器(服務器端)的數據庫,並從數據庫到文本框(客戶端)返回一個值。讓我知道這是否有助於澄清這一點,如果你能幫助。 – boilers222

+0

現在更有意義。除非劍道控制允許阿賈克斯更新,則需要自己更新(例如,使用Ajax調用服務器,以響應頁面上的變更,以獲得新的列表項)。 –

回答

0

jQuery是開始的好地方。如果我理解正確,則只需在更改下拉列表的值後查詢數據庫,然後將文本框的值更改爲相應的更改。

JQuery的:

$(document).ready(function(){ 
    $('#myDropDown').change(selectionChange()); 
}); 

function selectionChange() { 
    var dropDownValue = $('#myDropDown').val(); 
    var textBox = $('#myTextBox'); 

     $.ajax({ 
      url: "/mycontroller/querydb", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      data: JSON.stringify(dropDownValue), 
      success: function (data, status) { 
       textBox.val(data); 
      }, 
      type: "post" 
     }); 

    return; 
} 

控制器:

[HttpPost] 
    public JsonResult QueryDB(string dropDownValue) 
    { 
     string newTextBoxValue = string.Empty; 

     //your db code 

     return Json (newTextBoxValue)); 

    } 

這是一個jQuery AJAX的相當淡化的版本MVC控制器處理。希望它會爲你工作!

+0

感謝您的回覆;我會給這個鏡頭。無論如何,使用jquery ajax來更新整個模型而不是僅僅一個文本字段?爲了簡潔起見,我只在示例中包含了一個文本字段,但實際上有幾個需要更改。我希望當更改下拉選項時,可以更新整個模型的值。有什麼建議麼。 – boilers222

+0

我明白。聽起來您需要使用更多MVC的內置Ajax幫助器來提交表單的模型值,而不是發佈/刷新整個頁面。看看Ajax.BeginForm而不是HTML.BeginForm。我自己還沒有使用過,但它可以完美地爲您的目的服務。這裏有一個鏈接,給你一些好主意如何去做! http://stackoverflow.com/questions/15440041/mvc4-pass-model-via-ajax-beginform –

相關問題