0

I have created a custom html helper in MVC for MailTo functionality.

But I got an requirement to set the subject of the mail to the value user have entered in another textbox field.

I am not sure how to achieve this, Can some body please help?

Thanks

Html Helper

public static MvcHtmlString SendMailTo(this HtmlHelper helper, string emailAddress, string subject, string displayText)
    {
        var sb = string.Format("<a href=\"{0}{1}{2}\" title=\"{1}\">{3}</a>",
            CharEncode("mailto:"), CharEncode(emailAddress),CharEncode("?subject=" +subject), CharEncode(displayText));

        return new MvcHtmlString(sb);
    }

    public static string CharEncode(string value)
    {
        var enc = System.Text.Encoding.Default;
        var retval = "";
        for (var i = 0; i < value.Length; i++)
        {
            retval += "&#" + enc.GetBytes(new[] { Convert.ToChar(value.Substring(i, 1)) })[0] + ";";
        }
        return retval;
    }

View:

<div class="form-group">
                    @Html.LabelFor(m => m.ApplicationId, new { @class = "col-sm-3 control-label" })           
                    <div class="col-sm-8">
                        @Html.TextBoxFor(m => m.ApplicationId,  new {@class = "form-control"})          
                    </div>
                </div>          
              <div class="form-group">
                <div class="col-sm-11" style="text-align:right">
                    @Html.SendMailTo("info@test.com", "Password Request: ", "Request Password")                               
                    <button type="submit" class="button">Sign in</button>
                </div>
              </div>
Vin05
  • 587
  • 4
  • 12
  • 30

1 Answers1

0

You can not get that value without any jquery code.

You should make an event on keyup ( for example ) on your texbox ( this is the one which is used as an user mail input )

Put your email form in a partial view and update that partial when that event is triggered.

INFO :

A) To render your ParialView use can Html.Partial helper -> Example here

B) To get textbox value you can use following jquery code

    `$('.class of your textbox').val()' //will return a string

C) To update your partial view

   c.1 Place your partial view in a `div element`
   c.2 Make an `ajax` call from `jquery` to an action which returns your partial view filled with data

      Example for c.2:
           
           *AJAX* 

           $.ajax({
              type:"GET",
              data:{
                  mail:$('.class of your textbox').val()
              },
              success: function(response){
                 $(".class of your div").html(response.Html);
              }
              error: function(response){
                 // whatever you want to do 
              }
           });
           
          *Controller Action*

           public JsonResult(string mail)
           {

              var model = new CustomModel(){ Mail=mail };  // custom class to your as model for your partial view

              var html = RenderPartial(...)  // method you can find in the link posted below               
              return Json(
              {
                Html=html
              },JsonRequestBehavior.AllowGet)
           }
 

Link I was writing about few lines above - follow this link

You can call that ajax on whatever event you want for your textbox.

Community
  • 1
  • 1
Cosmin
  • 2,184
  • 21
  • 38
  • Many Thanks, Do you have any sample code which I can refer to? – Vin05 Apr 15 '14 at 13:38
  • Instead of adding partial view, can I just add some jquery code to send the email? – Vin05 Apr 15 '14 at 13:47
  • If you don't want to display destination mail somewhere else besides that textbox before sending, sure. Just make an onclick action for your link/button. PS: I've added some code – Cosmin Apr 15 '14 at 14:02
  • Thanks for adding the code, Yes this is the only page where I am having this functionality. my worry now is how to validate the form when user click on this link, I want user to enter the applicationid before they click on this link, I tried adding $('#LoginForm').validate(); but this is not validating the form. – Vin05 Apr 15 '14 at 15:01