如何从@ Html.DropDownList中获取选定的值

时间:2014-06-19 16:23:37

标签: c# asp.net-mvc

该项目是.Net framework3.5和MVC framework 1.0

在我看来,我有一个下拉列表

@Html.DropDownList("prodlist",@Model.products)

“添加产品”按钮

 <td> <input type ="button" name ="Add Product" value ="Add Product" id="Button1" /> </td>

和文本框@Html.TextBox ("prodselected")

当我点击“添加产品”按钮时,我应该能够在文本框“prodselected”中显示在Dropdownlist上选择的项目的值

请有人告诉我怎么做。如果现有代码中需要更改,请建议。

观点是:

<%@ Page Language="C#"     Inherits="System.Web.Mvc.ViewPage<MockBDPWorkflowTestApp.ViewModels.WorkFlowTestViewModel>" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

 <head id="Head1" runat="server">

<title>Submission</title>

 </head>

<body>

  <div>

    <% using (Html.BeginForm())
      {
        %>
        <div>
            <table>
             <tr>
              <td>
               <div id="SID">
                   <table id="tblID" cellpadding ="0" cellspacing ="0" border="1">
                    <tr>
                        <td width ="50%"> <label for="Process Inastance Id"> Process Inastance Id: </label> &nbsp;&nbsp; <%=@Html.TextBox("pInstance",@Model.processInstance) %></td>
                        <td width ="50%"> <label for ="Mail Id">Mail Id: </label> &nbsp;&nbsp; <%=@Html.TextBox("mail",@Model.mail) %> </td>
                    </tr>
                </table>
            </div>
            <br /><br />
            <table id="tblDet" cellpadding ="0" cellspacing ="0" >
                <tr>
                    <td width="50%"> <label for="Submission Number"> Submission Number: </label> &nbsp;&nbsp; <%=@Html.TextBox("sNo") %></td>
                    <td width ="50%"> <label for ="Name Insured">Name Insured: </label> &nbsp;&nbsp; <%=@Html.TextBox("nameInsured") %> </td>
                </tr>
                <tr>
                    <td width="50%"> <label for="Broker Code"> Broker Code: </label> &nbsp;&nbsp; <%=@Html.TextBox("brokerCode") %></td>
                    <td width ="50%"> <label for ="Broker Name">Broker Name: </label> &nbsp;&nbsp; <%=@Html.TextBox("brokerName") %> </td>
                </tr>
                 <tr>
                    <td width="50%"> <label for="Product Code"> Product Code: </label> &nbsp;&nbsp; <%=@Html.DropDownList("prodlist",@Model.products)%></td>
                     <td> <input type ="button" name ="Add Product" value ="Add Product" id="Button1" /> </td>
                </tr>
                <tr>
                    <td> <label for="Product Code"> Product Code Selected: </label> &nbsp;&nbsp; <%=@Html.TextBox ("prodselected")%></td>
                    <td> <input type ="submit"  value ="Submit Submission"  /> </td>
                </tr>
            </table>
         </td>
        </tr>
       </table>
    </div>
<% } %>
</div>

控制器是:

namespace MockBDPWorkflowTestApp.Controllers
{
    public class WorkFlowTestController : Controller
    {
        //
        // GET: /WorkFlowTest/

        public ActionResult OpenSubmission(string processId, string mailId)
        {
            var SubmissionModelView = new MockBDPWorkflowTestApp.ViewModels.WorkFlowTestViewModel{processInstance =processId,  mail =mailId} ;
            SubmissionModelView.products = new List<SelectListItem>();
            SubmissionModelView.products.Add(new SelectListItem { Text = "GL", Value = "0", Selected = true });
            SubmissionModelView.products.Add(new SelectListItem { Text = "Property", Value = "1" });
            SubmissionModelView.products.Add(new SelectListItem { Text = "Package", Value = "2" });
            SubmissionModelView.products.Add(new SelectListItem { Text = "Island Marine", Value = "3" });
            return View("Submission", SubmissionModelView);

        }
    }
}

ViewModel是:

   namespace MockBDPWorkflowTestApp.ViewModels
   {

      public class WorkFlowTestViewModel
      {
          public string processInstance { get; set; }
          public string mail { get; set; }
          public List<SelectListItem> products;
       }
   }

在视图中我添加了jquery函数,如下所示:

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MockBDPWorkflowTestApp.ViewModels.WorkFlowTestViewModel>"     %>
<script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('#Button1').click(function() {
        $('#prodlist').val($('#prodselected').val());
    });
  });
</script>

该函数没有问题,但prodlist没有显示任何值,而警报给了我一些垃圾jquery: 我也试过这个: $('#prodlist option:selected')。text;但是这也没有从prodlist中选择值,而是警报给了我一些垃圾jquery。

1 个答案:

答案 0 :(得分:5)

试试这个:

在viewmodel中创建一个属性,以存储下拉列表中的选定值

模型

public class WorkFlowTestViewModel
{
    public string processInstance { get; set; }
    public string mail { get; set; }
    public int product { get; set; }
    public List<SelectListItem> products;
}

视图

<td>
    <label for="Product Code">Product Code: </label>
    @Html.DropDownListFor(m => m.product, Model.products)</td>
<td>

控制器

[HttpPost]
public ActionResult OpenSubmission(WorkFlowTestViewModel model)
{
    //model.product is your dropdown value
    //your codegoes here
}

以下是 fiddle

相关问题