我正在创建一个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; }
更新:
我还应该补充一点,我将在发布时执行json序列化,因此它必须采用列表形式。理想情况下,我将能够对电子邮件标签类型的输入使用倍数,因为它将允许我需要的必要输入控件,而无需让我将其视为字符串并将其写入列表。
答案 0 :(得分: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);
}
答案 1 :(得分:0)
添加一个js文件,希望您使用jQuery
$(document).ready(function () {
$("#EmailList").removeClass("input-validation-error");
});
答案 2 :(得分:0)
查看kblau的答案后,我意识到部分原因。我遇到的另一个问题(MVC在其中进行了任何手动输入的客户端验证)是不干扰验证的结果:
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
对此进行注释后,它使我可以将输入写入字符串Email,然后将其分配给控制器中的字符串EmailList。最终为我工作。
答案 3 :(得分: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);
}