0

I am working on bulk sms in mvc. I have a view named SendSMS where i have fields like Sender ID, Numbers, Messagetext. Here, I am getting value of SenderID from Sender table where it consists Sender ID, Sender Name. Before sending message, user will add Sender Name from different view. As of now, I have added Sender Name. I am able to display Sender Name using dropdownList in SendSMS view.

After creating Sender ID and Sender Name, User enters SendSMS view to send Message. User will select Sender Name using dropdown list. He will add Numbers and Message text. When user clicks on Send button, the dropdown value is to passing to controller.

Here is the view code:

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

Controller:

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

`

Help would be appreciated.

user7090664
  • 45
  • 1
  • 9
  • What id your question? What problems are you having with your code? –  Mar 21 '17 at 09:36
  • @StephenMuecke The problem is in this line .From(sms.SendSMSModel.SenderId). The senderid value gets null. I m using dropdown to select sender Name. But, i am not able to pass the sender name here. – user7090664 Mar 21 '17 at 09:39
  • When I debug, all other values are passing from view to controller like Numbers, messagetext etc. Only SenderID is gettting null value – user7090664 Mar 21 '17 at 09:40
  • 1
    You not generating a form control for that property (for bind to that it needs to be `@Html.DropDownListFor(m => m.SendSMSModel.SenderId), new SelectList(...`. And do not use data models in your view. Use [view models](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Mar 21 '17 at 09:41
  • 1
    Try using `@Html.DropDownListFor(Model => Model.SendSMSModel.SenderId, new SelectList(ViewBag.SenderNameList, "SenderId", "SenderName"), "-Please Select-", new { @class = "form-control" })`. Your `SenderId` must be bound to view at this context to be passed into action method during POST. – Tetsuya Yamamoto Mar 21 '17 at 09:44
  • Yes. I changed it. Now, I want to pass SenderName from Dropdownlistfor to controller. Here I want to get SenderName **.From(sms.SendSMSModel.SenderId)**. How can I achieve that? – user7090664 Mar 21 '17 at 09:46
  • Look it up in the database based on the value of `SenderId` –  Mar 21 '17 at 09:48
  • @StephenMuecke Right now, i m getting senderId value in controller. Instead of that, I want to have SenderName. I am getting these senderdata from another table called db.Sender. How to have SenderName instead of SenderId value in controller? Can you check my controller code? – user7090664 Mar 21 '17 at 09:53
  • If you have LINQ to SQL/Entities mapping to DB, use `DB.Senders.Where(x => x.SenderId == sms.SendSMSModel.SenderId).Select(x => x.SenderName);` followed by `FirstOrDefault()` to get sender's name. – Tetsuya Yamamoto Mar 21 '17 at 09:54
  • 1
    If you do not want/need the `id`, then use `new SelectList(ViewBag.SenderNameList, "SenderName", "SenderName")` and bind to a view model property. –  Mar 21 '17 at 09:57
  • Thanks a lot. Working. I am able to send message to a particular number. How can i send message to a group of numbers? – user7090664 Mar 21 '17 at 10:21

1 Answers1

1
@Html.DropDownListFor(model => model.SendSMSModel.SenderId, new SelectList(ViewBag.SenderNameList, "SenderId", "SenderName"), "-Please Select-", new { @class = "form-control" })
Roger
  • 76
  • 4
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Mar 21 '17 at 11:43