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

本文关键字:设置 文本 Mailto 功能 | 更新日期: 2023-09-27 17:58:29

我在MVC中为MailTo功能创建了一个自定义的html助手。

但我得到了一个要求,将邮件的主题设置为用户在另一个文本框字段中输入的值。

我不知道如何做到这一点,可以请一些身体帮助吗?

感谢

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;
    }

视图:

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

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

如果没有任何jquery code,就无法获得该值。

您应该在texbox(这是一个用作用户邮件输入的)上的keyup(例如)上创建一个事件

将您的email form放入partial view中,并在触发该event时更新该partial

信息:

A) 要渲染ParialView,请使用can Html.Partial helper->示例

B) 要获得文本框值,您可以使用以下jquery代码

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

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)
           }
 

链接我在上面写了几行-点击这个链接

您可以在任何您想要的textbox事件中调用该ajax