0

我正在创建一个 MVC 应用程序,其中我将有一个电子邮件列表的输入字段。为此,我添加了多个以允许用户输入以逗号分隔的电子邮件列表。通过这种方式,我可以使用输入控件来检查电子邮件的格式是否正确(“.+@gmail.com”)。

问题是,当我对此进行测试时,它会自动添加 class="input-validation-error" (即使我之前设置了 @class="" )并且由于输入无效而不允许我发布,因此. 有什么方法可以做到这一点,还是我唯一的选择是将其设为电子邮件字符串属性并通过逗号将其解析为控制器中的 EmailList 属性?

(这是我的代码):

看法:

@Html.TextBoxFor(model => model.EmailList, new { type = "email", placeholder 
= "ex@gmail.com (',' Delimited)", title = "Make sure your email(s) are 
formatted appropriately (and comma separated).", multiple = "" })    

模型:

public List<string> EmailList { get; set; }    

更新:

我还应该补充一点,我在 post 上执行 json 序列化,所以它需要采用列表的形式。理想情况下,我将能够将倍数用于类型电子邮件标签的输入,因为它允许我需要的必要输入控件,而无需将其作为字符串并将其写入列表。

4

4 回答 4

0

新的小提琴在这里,点击它https://dotnetfiddle.net/ORYDVJ

看法

@model Testy20161006.Controllers.MurphyModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut122</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#emailField").change(function () {
                var theList = {
                    emaillist: []
                };
                var array = $('#emailField').val().split(",");
                $.each(array, function (i) {
                    theList.emaillist.push(
                        array[i]
                    );
                });
                $.ajax({
                    url: '/Home/Tut122',
                    traditional: true,
                    type: "POST",
                    contentType: "application/json",
                    data: JSON.stringify({ murphyModel: theList }),
                    success: function (data) {
                        console.log('success!!');
                        $("#theOutput").html(data)
                    }
                });
            })
        })
    </script>
</head>
<body>
    @Html.TextBoxFor(model => model.EmailList, new
            {
                id = "emailField",
                type = "email",
                placeholder = "ex@gmail.com (',' Delimited)",
                title = "Make sure your email(s) are formatted appropriately (and comma separated).",
                multiple = ""
            })
    <span>The output data:</span>
    <div id="theOutput">
    </div>
</body>
</html>

控制器/视图模型

public class MurphyModel
{
    public List<string> EmailList { get; set; }
}

public class HomeController : Controller
{
    [HttpPost]
    public string Tut122(MurphyModel murphyModel)
    {
        //You need to get Newtonsoft.JSON
        var json = JsonConvert.SerializeObject(murphyModel);
        return json;
    }

    public ActionResult Tut122()
    {
        MurphyModel model = new MurphyModel();
        return View(model);
    }
于 2018-09-25T21:30:33.103 回答
0

https://dotnetfiddle.net/eadTjh

看法

@model Testy20161006.Controllers.MurphyModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut122</title>
</head>
<body>
    @if (ViewBag.Output != null)
    {
        <span>@ViewBag.Output</span>
    }
    @using (Html.BeginForm())
    {
        @Html.TextBoxFor(model => model.EmailList, new
        {
            type = "email",
            placeholder = "ex@gmail.com (',' Delimited)",
            title = "Make sure your email(s) are formatted appropriately (and comma separated).",
            multiple = ""
        })
        <input type="submit" value="submit" />
    }
</body>
</html>

控制器/视图模型

namespace Testy20161006.Controllers
{
public class MurphyModel
{
    //We don't want a list class, rather a string 
    public string EmailList { get; set; }
}

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Tut122(MurphyModel model)
    {
        var splitEmails = model.EmailList.Split(',');
        var anEmail = "These are the different emails: ";
        foreach (string email in splitEmails)
        {
            //set breakpoint here
            anEmail+= email + " and ";
        }
        anEmail = anEmail.Substring(0, anEmail.Length - 5);
        ViewBag.Output = anEmail;
        return View(model);        }

    public ActionResult Tut122()
    {
        MurphyModel model = new MurphyModel();
        return View(model);
    }
于 2018-09-24T20:22:49.873 回答
0

在查看 kblau 的答案后,我意识到这部分是原因。我遇到的另一个问题(MVC 跳过了我手动输入的任何客户端验证)是不显眼的验证的结果:

<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>    

在将其注释掉之后,它允许我将输入写入字符串 Email,然后我将其分配给控制器中的字符串列表 EmailList。这最终为我工作。

于 2018-09-25T15:00:19.427 回答
0

添加一个 js 文件,希望你使用 jQuery

$(document).ready(function () {
    $("#EmailList").removeClass("input-validation-error");

});
于 2018-09-24T20:44:16.157 回答