将值从视图传递到控制器MVC4.0 c#

问题描述 投票:0回答:1

我是MVC的新手,并且可以查看以下搜索记录

  @using (Html.BeginForm("SearchRecord", "Home", FormMethod.Post))
        {
            <table class="table table-striped">                   
                <tbody>
                    <tr>
                        <td>
                            @Html.TextBox("txtCallerId",  Model.callerNo, new { placeholder = "Caller Number", @class = "form-control" })

                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div id="divlstCampaigns">
                                <select id="lstCampaigns" name="lstCampaigns[]" multiple="multiple">
                                    @foreach (var item in Model.cmpList)
                                    {
                                        <option value='@item.campaignName'>@item.campaignName</option>
                                    }
                                </select>
                            </div>
                        </td>
                    </tr>                        
                    <tr>
                        <td><button type="submit" class="btn btn-default bg-info" value="Submit">Search</button>  </td>
                    </tr>
                </tbody>
            </table>
        }

和控制器

    public ActionResult SearchRecord()
    {
        string userName = TempData["user"] as string;
        instance = TempData["instance"] as IInstance;
       ......
        lstSearchCriteria objSearc = new lstSearchCriteria();
        objSearc.agentList = agentList;
        objSearc.cmpList = campaignList;

        return View(objSearc);
    }      
    [HttpPost]
    public ActionResult SearchRecord(lstSearchCriteria form)
    {
        List<lstCampaign> lstCmp = form.cmpList;//NULL Value here
        string callerNo = form.callerNo;//NULL Value here

        return View();
    }

我的模特是:

  public class lstSearchCriteria
  {
    public List<lstCampaign> cmpList { get; set; }     
    public string callerNo { get; set; }
    // ......
  }

我希望当我单击提交或搜索按钮时。文本框和下拉框中的值应在控制器中可见。但是我得到所有空值。

asp.net-mvc view controller onclick submit
1个回答
0
投票

您可以尝试这种方式:

视图:

<select id="cmpList" name="cmpList" multiple="multiple">

lstSearchCriteria

public int[] cmpList { get; set; }  

This链接对您非常有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.