我有一個使用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;"> </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);
}
感謝您的答覆,但是這不是我做什麼都沒有。下拉列表中沒有我想要放入文本框的值,並且客戶端上沒有任何值具有該值。我想借此從下拉列表(客戶端)的值,查詢控制器(服務器端)的數據庫,並從數據庫到文本框(客戶端)返回一個值。讓我知道這是否有助於澄清這一點,如果你能幫助。 – boilers222
現在更有意義。除非劍道控制允許阿賈克斯更新,則需要自己更新(例如,使用Ajax調用服務器,以響應頁面上的變更,以獲得新的列表項)。 –