我有以下课程:
public class ClinicaViewModel
{
public List<String> EmailProfissionais { get; set; }
public string Email { get; set; }
public string Fantasia { get; set; }
}
使用相应的控制器:
public ActionResult Create()
{
return View(new ClinicaViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(ClinicaViewModel clinica)
{
if (ModelState.IsValid)
{
\\ Some code here
}
return View(clinica);
}
该视图使用以下代码以Javascript形式将动态生成的字段生成为先前未知数量的值:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
\\ Some code here...
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Gravar" class="btn btn-primary" />
</div>
</div>
<p></p>
<div id="listaProfissionais"></div>
<p><br /></p>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button class="btn btn-success js-addProfissional">Adicionar profissional</button>
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/cookies.js"></script>
<script>
$(document).ready(function () {
Cookies.set('6172D459-29BB-4874-B418-1D9AACEC095F', 0);
$(".js-addProfissional").click(function (event) {
let iteration = parseInt(Cookies.get('6172D459-29BB-4874-B418-1D9AACEC095F'));
iteration++;
Cookies.set('6172D459-29BB-4874-B418-1D9AACEC095F', iteration);
event.preventDefault();
addDom();
});
});
function addDom() {
let node = "<p></p>";
node += "<div class='input-group mb-3'>";
node += "<input class='form-control' placeholder='E-mail do usuário' name='Email";
node += Cookies.get('6172D459-29BB-4874-B418-1D9AACEC095F');
node += "' type='text' Id='Email";
node += Cookies.get('6172D459-29BB-4874-B418-1D9AACEC095F');
node += "'/> ";
node += "</div>";
$("#listaProfissionais").append(node);
};
</script>
}
cookies.js是用来处理Cookie的库...
在一个示例中,其中生成了以下HTML的3个字段:
<div id="listaProfissionais">
<p></p>
<div class="input-group mb-3">
<input class="form-control valid" placeholder="E-mail do usuário" name="Email1" type="text" id="Email1" aria-invalid="false">
</div>
<p></p>
<div class="input-group mb-3">
<input class="form-control valid" placeholder="E-mail do usuário" name="Email2" type="text" id="Email2" aria-invalid="false">
</div>
<p></p>
<div class="input-group mb-3">
<input class="form-control valid" placeholder="E-mail do usuário" name="Email3" type="text" id="Email3" aria-invalid="false">
</div>
因此,在上面的示例中,我应该接收3个输入字段中的3个值,在一般情况下,我应该接收所生成的尽可能多的值。
问题在于,尽管该类具有定义public List<String> EmailProfissionais { get; set; }
,但我仍收到一个空对象。
如何接收所产生的所有输入字段的结果?