读取控制器中下拉列表的选定值

发布于 2024-12-24 20:43:06 字数 2261 浏览 0 评论 0原文

要求:我的 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);
    }

Requirment: I have a drop down and a table on my cshtml page. The drop down displays a list of vendors and the details corresponding to selected vendor are displayed in table. I am submitting the form using jquery when the value of the drop down changes.

Problem: How to cath selected value of drop down in controller?

Code:

@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>

the code in my controller is:

    [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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-12-31 20:43:06

由于您并未真正使用 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>

Edit< /strong>

看一下这篇文章,了解 MVC 的模型绑定vendorId 如何从提交的表单中注入的想法。基本上,绑定器会将具有 name 属性(默认情况下)的属性名称与您的模型进行匹配。在本例中,我们的模型只是一个 int

Since you're not really using the FormCollection, you could just use an int (or whatever the ID is on your model) in your action method:

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

In your HTML, move your @Html.DropDownListFor() into your form, rename it to the argument name, then submit the form as normal. Since the display doesn't seem to have any affect on what gets sent to the server, I would pull this out and just leave the @Html.DropDownListFor() in the form:

@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>

Edit

Take a look at this article about MVC's model binding for an idea of how vendorId gets injected from the submitted form. Basically, the binder will match property names with the name attribute (by default) to your model. In this case, our model is just an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文