【发布时间】:2015-07-10 20:29:16
【问题描述】:
我有一个使用 Kendo 控件的 MVC 项目。其中一个视图是下拉框和文本框。两者最初都是从模型中获取它们的值。当用户从下拉列表中选择一个项目时,如何更改模型(以及文本框)?
例如,在控制器中填充模型,将下拉框所基于的项目的原始值设置为“General”,将文本框所基于的项目设置为“Widgets”。当用户从下拉菜单中选择“Special”时,控制器会根据“Special”查询数据库获取数据,发现文本框的新值应该是“Doodads”,将“Doodads”添加到模型中并更改文本框到“Doodads”。
查看
@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);
}
【问题讨论】:
-
感谢您的回复,但这根本不是我要做的。下拉列表没有我想要放入文本框中的值,并且客户端上没有任何位置具有该值。我想从下拉列表中获取值(客户端),在控制器中查询数据库(服务器端),然后将数据库中的值返回到文本框(客户端)。让我知道这是否有助于澄清这一点以及您是否可以提供帮助。
-
现在更有意义了。除非 Kendo 控件允许 Ajax 更新,否则您将需要自己更新它(例如,使用对服务器的 Ajax 调用,以响应页面上的更改,以获取新的列表项)。
标签: javascript jquery asp.net-mvc asp.net-mvc-4 jquery-ui