[英]ASP.NET Core MVC post model with IEnumerable or List fields are null
I need help to find out the next error.我需要帮助来找出下一个错误。 I'm trying to post model with List parameters.我正在尝试使用列表参数发布 model 。 But, when I receive the list this has a null value.但是,当我收到列表时,它有一个 null 值。
Model class: Model class:
public class DiagModel{
public List<Component> components { get; set; }
public List<Question> questions { get; set; }
public List<Answer> answers { get; set; }
public List<Questionnaire> forms { get; set; } //it populates after instatiate object from the questions
//forms->(The propierties are idQuestion and idAnswer)
}
The view is shown below (@Html.hiddenFor() for each property from objects in fors were omitted):该视图如下所示(省略了fors中对象的每个属性的@Html.hiddenFor()):
@using Test.Models.compounds;
@model DiagModel;
@{
Layout = "~/Views/Shared/_LayoutCommon.cshtml";
}
@if (!string.IsNullOrEmpty(ViewBag.Message))
{
<script type="text/javascript">
alert("@ViewBag.Message");
</script>
}
@using (Html.BeginForm(new { @id = "requestForm" }))
{
@for (int i = 0; i < Model.components.Count(); i++)
{
//The hidden components with razor helper was deleted to make short the code [Component]
<div class="row text-center">
<div class="col-md-10 offset-md-1 text-start" style="background-color: #dddddd">
<span class="text-start">@Model.components[i].name</span>
</div>
</div>
<div class="row"> </div>
List<Question> _questions = Model.questions.Where(p => p.Idcomponent == Model.components[i].Idcomponent).ToList();
for (int j = 0; j < _questions.Count; j++)
{
//The hidden components with razor helper was deleted to make short the code [Question]
<div class="row p-1">
<div class="col-md-10 offset-md-1 text-start">
<label class="form-label fw-bold">@_questions[j].Idquestion @_questions[j].Textopregunta</label>
@Html.DropDownListFor(model => model.forms.Where(c => c.Idquestion == @_questions[j].Idquestion).First().Idanswer, new SelectList(Model.answers.Where(r => r.Idquestion == @_questions.ElementAt(j).Idquestion), "Idquestion", "Text"), "Select an option", new { @class = "form-control" })
</div>
</div>
<div class="row"> </div>
}
}
<div class="row p-1 text-center">
<div class="col-md-12">
<button name="submit" type="submit" class="btn btn-danger" formaction="Question" value=@Model>Enviar</button>
</div><!--end col -->
</div>
}
<div class="row p-2"> </div>
The controller gets the value but the collection is received as null. controller 获取值,但集合作为 null 接收。
[HttpPost]
public ActionResult Question(DiagModel model){
//ViewBag.Message($"Q #1: {model.forms.First().IdQuestion} , R #1: {model.forms.First().IdAnswer}");
ViewBag.Message = $"count: {model.forms.Count}";//Breakpoint
if (ModelState.IsValid) {
return View(model);
}
return View("Index");
}
You can only send input elements with form, or you can send them as parameters with javascript xhr request.您只能使用表单发送输入元素,也可以使用 javascript xhr 请求将它们作为参数发送。
Most important thing about razor is input element [name] properties need to match the model field.关于 razor 最重要的是输入元素 [名称] 属性需要匹配 model 字段。
<input type="text" name="questions[1].Id" class="form-label fw-bold" value="@Model.questions[1].Id" />
If you working on nested objects, it has to be reflected on Model that you try to bind on the server side as well.如果您处理嵌套对象,则它必须反映在您尝试在服务器端绑定的 Model 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.