【问题标题】:Read selected value of drop down in controller读取控制器中下拉列表的选定值
【发布时间】:2012-02-02 15:17:23
【问题描述】:

要求:我的 cshtml 页面上有一个下拉列表和一个表格。下拉列表显示供应商列表,与所选供应商对应的详细信息显示在表格中。当下拉列表的值发生变化时,我正在使用 jquery 提交表单。

问题:如何在控制器中设置下拉列表的选定值?

代码:

@Html.DropDownList("VendorList", new SelectList(Model.vendorList, "vendorId", "vendorName"))

@using (Html.BeginForm("VendorDetails", "VendorLookUp", FormMethod.Post, new { id = "vendorDetailsForm" }))
{
    <div class="margin-10-top" >
      <table id= "VendorDetail" class="VendorDetail">

        ........ details of vendor.........

      </table>
   </div>
}

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
    $('#VendorList').change(function () {
        $('#vendorDetailsForm').submit();
    });
}); 
</script>

我的控制器中的代码是:

    [AcceptVerbs("POST")]
    public ActionResult SearchResult(FormCollection collection)
    {
        try
        {
            string vendorName = collection["searchItem"].ToString();

            vendorName = vendorName.Trim();
            List<Vendor> vendorList = Queries.compiledVendorQuery(dbContext, vendorName).ToList<Vendor>();

            if(vendorList.Count() == 0)
                return View("EmptySearch");

            Vendor selectedVendor = vendorList[0];

            VendorDetails vendorDeatils = Queries.compiledVendorDetailsQuery(dbContext, selectedVendor.vendorId.ToString()).FirstOrDefault();

            VendorResult vendorResult = new VendorResult();
            vendorResult.vendorList = vendorList;
            vendorResult.vendorDetails = vendorDeatils;

            return View(vendorResult);
        }
        catch (Exception e)
        {
            return View("EmptySearch");
        }
    }

    [AcceptVerbs("POST")]
    public ActionResult VendorDetails(FormCollection collection)
    {
        **here comes the control after postback
        require value of the selected item** 

        Vendor selectedVendor = ?? 

        VendorDetails vendorDeatils = Queries.compiledVendorDetailsQuery(dbContext, selectedVendor.vendorId.ToString()).FirstOrDefault();

        VendorResult vendorResult = new VendorResult();
        vendorResult.vendorDetails = vendorDeatils;

        return View(vendorResult);
    }

【问题讨论】:

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


    【解决方案1】:

    由于您并没有真正使用FormCollection,您可以在您的操作方法中使用int(或您模型上的任何ID):

    [HttpPost]
    public ActionResult VendorDetails(int vendorId = 0)
    {
        Vendor selectedVendor = // Load from your data source using vendorId
        ...  // Do the rest of your work
    }
    

    在您的 HTML 中,将您的 @Html.DropDownListFor() 移动到您的表单中,将其重命名为参数名称,然后正常提交表单。由于显示似乎对发送到服务器的内容没有任何影响,因此我将其拉出并仅将 @Html.DropDownListFor() 留在表单中:

    @using (Html.BeginForm("VendorDetails", "VendorLookUp", FormMethod.Post, new { id = "vendorDetailsForm" }))     
    {
        @Html.DropDownList("vendorId", new SelectList(Model.vendorList, "vendorId", "vendorName"))
    }
    
    <div class="margin-10-top" >
        <table id= "VendorDetail" class="VendorDetail">
            ........ details of vendor.........
        </table>
    </div>
    
    <script type='text/javascript'>
        $(document).ready(function () {        
            $('#vendorId').change(function () {
                $('#vendorDetailsForm').submit();
            });
        });
    </script>
    

    编辑

    查看 this article 关于 MVC 的模型绑定,了解如何从提交的表单中注入 vendorId。基本上,活页夹将属性名称与name 属性(默认情况下)匹配到您的模型。在这种情况下,我们的模型只是一个int

    【讨论】:

    • 这是我的问题.. 我将如何在控制器中获得这个选定的 id 值?
    • 按照惯例。阅读有关模型绑定器如何处理此问题的更多信息。
    • @Mini 是的,上面的例子可以为你工作,但是如果你需要另一种使用 jquery 的方法,那么我会给你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多