如何在mvc中将下拉值传递给控制器​​?

时间:2017-03-21 09:34:53

标签: c# asp.net-mvc asp.net-mvc-4

我正在研究mvc中的批量短信。我有一个名为SendSMS的视图,其中包含Sender ID,Numbers,Messagetext等字段。在这里,我从Sender表中获取SenderID的值,它包含发件人ID,发件人名称。在发送消息之前,用户将从不同的视图添加发件人名称。截至目前,我已添加了发件人名称。我可以使用SendSMS视图中的dropdownList显示发件人名称。

创建发件人ID和发件人名称后,用户输入SendSMS视图以发送邮件。用户将使用下拉列表选择发件人名称。他将添加Numbers和Message文本。当用户单击“发送”按钮时,下拉值将传递给控制器​​。

以下是视图代码:

@using (Html.BeginForm("SendMessage", "SendSMS", FormMethod.Post, new { enctype = "multipart/form-data" }))
{                              
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>Compose New Message</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(Model => Model.SendSMSModel.SenderType, new { @class = "form-control-label" })
        <div class="input-group">
            <span class="input-group-addon input_email">
                <i class="fa fa-user text-primary"></i>
            </span>
            @Html.EnumDropDownListFor(Model => Model.SendSMSModel.SenderType, new { @class = "form-control  form-control-md" })
            @Html.ValidationMessageFor(Model => Model.SendSMSModel.SenderType, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(Model => Model.SendSMSModel.SenderId, new { @class = "form-control-label" })
        <div class="input-group">
            <span class="input-group-addon input_email">
                <i class="fa fa-sort-numeric-asc text-primary"></i>
            </span>                                  
            @Html.DropDownList("SenderId", new SelectList(ViewBag.SenderNameList, "SenderId", "SenderName"), "-Please Select-", new { @class = "form-control" })
            @Html.ValidationMessageFor(Model => Model.SendSMSModel.SenderId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(Model => Model.SendSMSModel.Numbers, new { @class = "form-control-label" })
        <div class="input-group">
            <span class="input-group-addon input_email">
                <i class="fa fa-sort-numeric-asc text-primary"></i>
            </span>
            @Html.TextBoxFor(Model => Model.SendSMSModel.Numbers, new { @class = "form-control  form-control-md" })
            @Html.ValidationMessageFor(Model => Model.SendSMSModel.Numbers, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SendSMSModel.MessageText, new { @class = "form-control-label" })
        <div class="input-group">
            <span class="input-group-addon input_email">
                <i class="fa fa-file-text text-primary"></i>
            </span>
            @Html.EditorFor(model => model.SendSMSModel.MessageText, new { htmlAttributes = new { @class = "form-control", @onkeyup = "javascript:ValidateCharactercount(this);", @rows = "10", @cols = "50" } })
            @Html.ValidationMessageFor(model => model.SendSMSModel.MessageText, "", new { @class = "text-danger" })
        </div>
        <div id="divmessage" style="color: Red;">
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Send Now" class="btn btn-info" />
        </div>
    </div>
</div>

控制器:

`[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SendMessage(SMS_SignatureModel sms)
        {
            Inetlab.SMPP.SmppClient client = new Inetlab.SMPP.SmppClient();


            client.Connect("***.**.***.**", ****);

            if (client.Status == ConnectionStatus.Open)
            {
                client.SystemType = "****";
                client.Bind("******", "******", ConnectionMode.Transmitter);

                if (client.Status == ConnectionStatus.Bound)
                {
                    IList<SubmitSmResp> respList = client.Submit(
                        SMS.ForSubmit()
                        .From(sms.SendSMSModel.SenderId)
                        .To(sms.SendSMSModel.Numbers)
                        .Text(sms.SendSMSModel.MessageText)
                        .DeliveryReceipt()
                        .Coding(DataCodings.Default)
                        );

                    if (respList.Count > 0 && respList[0].Status == CommandStatus.ESME_ROK)
                    {
                        Console.WriteLine("SMS has been sent");
                        foreach (SubmitSmResp resp in respList)
                        {
                            Console.WriteLine("MessageId: " + resp.MessageId);
                        }
                    }

                    client.UnBind();
                }

                client.Disconnect();
            }
            return RedirectToAction("SendMessage", "SendSMS");
        }

`

帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

@Html.DropDownListFor(model => model.SendSMSModel.SenderId, new SelectList(ViewBag.SenderNameList, "SenderId", "SenderName"), "-Please Select-", new { @class = "form-control" })