【问题标题】:In an MVC project, how do you update the model when a drop down list changes value?在 MVC 项目中,当下拉列表更改值时如何更新模型?
【发布时间】: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;">&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);
}

【问题讨论】:

  • 感谢您的回复,但这根本不是我要做的。下拉列表没有我想要放入文本框中的值,并且客户端上没有任何位置具有该值。我想从下拉列表中获取值(客户端),在控制器中查询数据库(服务器端),然后将数据库中的值返回到文本框(客户端)。让我知道这是否有助于澄清这一点以及您是否可以提供帮助。
  • 现在更有意义了。除非 Kendo 控件允许 Ajax 更新,否则您将需要自己更新它(例如,使用对服务器的 Ajax 调用,以响应页面上的更改,以获取新的列表项)。

标签: javascript jquery asp.net-mvc asp.net-mvc-4 jquery-ui


【解决方案1】:

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 控制器交易的版本。希望它对你有用!

【讨论】:

  • 感谢您的回复;我会试一试。反正有没有使用 jquery ajax 来更新整个模型,而不仅仅是一个文本字段?为了简洁起见,我在示例中只包含了一个文本字段,但实际上有几个需要更改。我希望当下拉选择发生变化时,可以更新整个模型的值。任何建议。
  • 我明白了。听起来您需要更多地使用 MVC 的内置 Ajax 助手来提交表单的模型值,而不是发布/刷新整个页面。看看 Ajax.BeginForm 而不是 HTML.BeginForm。我自己还没有使用过这个,但它可以完美地满足你的目的。这是一个链接,可让您了解如何去做! stackoverflow.com/questions/15440041/…
猜你喜欢
  • 1970-01-01
  • 2013-04-24
  • 2014-04-14
  • 2011-02-19
  • 2018-02-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多