【问题标题】:I want to Populate a Dropdown list and fill a textbox accordingly?我想填充下拉列表并相应地填充文本框?
【发布时间】:2012-08-01 17:53:17
【问题描述】:

我有两个下拉列表,我想要,如果我从这两个下拉列表中选择一些东西,第三个文本框将使用 java 脚本自动填充... 这是我的控制器..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceSuite.Web.Models.Product;
using CommerceSuite.Services;
using CommerceSuite.Web.Models.AdjustStock;
using CommerceSuite.Data.Models;

namespace CommerceSuite.Web.Controllers
{
public class AdjustStockController : Controller
{
    private readonly IProductStockService _productStock;
    private readonly IProductService _product;
    private readonly ILocationDetailService _locationDetail;
    public AdjustStockController(IProductStockService productStock, IProductService product, ILocationDetailService locationDetail)
    {
        this._productStock = productStock;
        this._product = product;
        this._locationDetail = locationDetail;
    }
    //
    // GET: /AdjustStock/

    public ActionResult Index()
    {
        var model = new AdjustStockModel();
        model.products = PopulateProductId();
        model.locations = PopulateLocation();
        return PartialView("_AdjustStock", model);
    }

    [HttpGet]
    public ActionResult StockAdjust()
    {
        var model = new AdjustStockModel();
        return PartialView("_AdjustStock",model);
    }

    private List<SelectListItem> PopulateProductId()
    {
        var collectionProduct = new SelectList(_product.GetAllProduct(), "ProductId", "Name").ToList();
        return collectionProduct;
    }
    private List<SelectListItem> PopulateLocation()
    {
        var collectionLocation = new SelectList(_locationDetail.GetAllLocationDetail(), "LocationId", "Name").ToList();
        return collectionLocation;
    }

    CommerceSuiteWMDBContext context = new CommerceSuiteWMDBContext();
    public JsonResult GetData(AdjustStockModel model)
    {
       var data = context.ProductStocks.Where(o => o.ProductId == model.ProductId && o.LocationId == model.LocationId);
       var data = (from t in context.ProductStocks
                   where t.ProductId == model.ProductId && t.LocationId == model.LocationId
                    select t).SingleOrDefault();
        var jsonData = new
        {
            quantity=data.QuantityAvailable
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

这是我的观点..

@model CommerceSuite.Web.Models.AdjustStock.AdjustStockModel



@Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.ProductId)

    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.ProductId, Model.products, "Select Product")
        @Html.ValidationMessageFor(model => model.ProductId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.LocationId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model=>model.LocationId,Model.locations,"Select Location")
        @Html.ValidationMessageFor(model => model.LocationId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SystemStock)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model=>model.SystemStock)
        @Html.ValidationMessageFor(model => model.SystemStock)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ActualStock)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model=>model.ActualStock)
        @Html.ValidationMessageFor(model => model.ActualStock)
    </div>

    <br />

    <div class="block-footer align-right">
        <input type="submit" id="popupConfirmBtn" class="big-button" value="Confirm" />
        <input id="popupCloseBtn" type="button" class="big-button" value="Cancel" onclick="csPopupClose()" />
    </div>

    <div id="popLoading" style="display:none; position:absolute; margin-top:-180px; margin-left:150px; border:none;">
        <img src="../../Content/images/loader.gif" alt="Loading" />
    </div>

现在我如何编写一个 java 脚本来实现这个功能..这样当我选择任何 Dropbox 时,一个文本框会根据 Json 返回的值自动填充。

【问题讨论】:

  • 实际上我不知道在 lambda 表达式的情况下如何从下拉列表中将 id 传递给 java 脚本。

标签: javascript asp.net-mvc asp.net-mvc-3 model-view-controller


【解决方案1】:

由于您使用的是 MVC,所以我会看看 Knock Out MVC library。您可以通过 NuGet 添加它。 This example 有一个更新div 的下拉列表。并且在线上有很多文档齐全的示例和教程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    相关资源
    最近更新 更多