简体   繁体   English

在Mailto功能中将文本框值设置为主题。

[英]Setting the text-box value as the subject in Mailto functionality.

I have created a custom html helper in MVC for MailTo functionality. 我已经在MVC中为MailTo功能创建了一个自定义html帮助器。

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 HTML助手

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>

You can not get that value without any jquery code . 没有任何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 ) 您应该在texbox上的keyup(例如)上创建一个事件(这是用作用户邮件输入的事件)

Put your email form in a partial view and update that partial when that event is triggered. 将您的email form置于partial view并在触发该event时更新该partial

INFO : 信息:

A) To render your ParialView use can Html.Partial helper -> Example here A)使用Html.Partial helper > 此处的示例渲染ParialView

B) To get textbox value you can use following jquery code B)要获取文本框值,可以使用以下jquery代码

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

C) To update your partial view C)更新您的局部视图

   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 . 您可以在您希望textbox发生的任何事件中调用该ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM