将所选列表框项目发送到控制器

时间:2013-04-10 11:19:54

标签: asp.net-mvc razor listbox

我的情景就在这里       好的我有一个多选列表框,我想要的是当我们选择多个记录时       并按下按钮,我应该在调用方法中选择项目列表       控制器。我的代码在下面

csthml代码:

@{
 Layout = "";
}
@using Telerik.Web.Mvc.UI;
@using System.Web;
@using System.Web.UI.WebControls;
@using System.Web.Mvc;

@model Nop.Plugin.Import.Product.Models.ImportProductModel
<div>
<div class="section-header">
    <div class="options">            
    @using (Html.BeginForm("ImportSelected",  "ImportProduct"))
    {
        <button type="submit">OK</button>
    }
    </div>
 </div>

 <table>
    <tr>
        <td>
            @Html.ListBox("Items", Model.nopCommerceCategories);
        </td>

        <td>
            @Html.ListBox("Items", Model.ClockCategories);
        </td>
    </tr>
    <tr>
        <td>
                @using (Html.BeginForm("SaveMapping", "ImportProduct"))
                {
                    <button type="submit">Save</button>
                }
        </td>
    </tr>
  </table>        
 </div>

型号:

using System.Collections.Generic;
using System.Web.Mvc;
using Nop.Core.Domain.Catalog;

namespace Nop.Plugin.Import.Product.Models
{
    public class ImportProductModel
    {        
        public List<SelectListItem> nopCommerceCategories { get; set; }
        public List<SelectListItem> ClockCategories { get; set; }
    }
}

2 个答案:

答案 0 :(得分:0)

注意:列表框的第一个参数是您要发送给控制器的名称!因此,您应该使用此类代码:

@Html.ListBox("nopCommerceCategories", Model.nopCommerceCategories);
@* or this code :
@Html.ListBoxFor(model => model.nopCommerceCategories);
*@

然后当您使用以下签名呼叫控制器时:

 public ActionResult SaveMapping(ImportProductModel ipm) {
     // here you have access to ipm.nopCommerceCategories
 }

此外,您必须使用要在一个表单中获取的数据封装所有代码! 如果您想要提交多个操作,请添加一个javascript代码,以便检索正确的操作以提交您的数据...

答案 1 :(得分:0)

你的表单里面只有一个提交按钮,你应该将你的列表框放在一个表单中,当表单被提交时,列表框的值和表单中的所有其他内容将被发送到表格指向

相关问题