[英]Passing a List from View to Controller in Model
I am trying to pass a complex data structure from Controller to View and Back to Controller which contains Lists.我正在尝试将复杂的数据结构从控制器传递到视图并返回到包含列表的控制器。 I can see the list items in View.我可以在视图中看到列表项。 I want to edit those and send it back to the controller.我想编辑这些并将其发送回控制器。 I am able to edit some properties but for lists, I am getting null value in the controller.我可以编辑一些属性,但对于列表,我在控制器中获得了空值。
Here is an example (simulation) of what I am trying to achieve:这是我试图实现的示例(模拟):
Consider Model -考虑模型 -
using System.Collections.Generic;
namespace WebApplication1.Models
{
public class StudentViewModel
{
public string StudentId { get; set; }
public string FeedBack { get; set; }
public List<ScoreCard> ScoreCards;
}
public class ScoreCard
{
public string Subject { get; set; }
public string Marks { get; set; }
}
}
Controller -控制器 -
public class StudentController : Controller
{
public ActionResult Index()
{
var model = new StudentViewModel();
model.StudentId = "Some Student";
model.ScoreCards = new List<ScoreCard>
{
new ScoreCard()
{
Marks = "0",
Subject = "English"
},
new ScoreCard()
{
Marks = "0",
Subject = "Maths"
}
};
return View("View", model);
}
public ActionResult SubmitScore(StudentViewModel model)
{
/* Some Code */
}
}
View -看法 -
@model WebApplication1.Models.StudentViewModel
@{
ViewBag.Title = "Title";
}
@using (Html.BeginForm("SubmitScore", "Student", FormMethod.Post))
{
@Html.DisplayName(@Model.StudentId)<br />
<label>Comment:</label><br/>
@Html.EditorFor(m => m.FeedBack, new { htmlAttributes = new { @type = "text", id = @Model.FeedBack} })<br />
for (var i = 0; i < @Model.ScoreCards.Count; i++)
{
@Html.DisplayName(@Model.ScoreCards[i].Subject) <br/>
@Html.EditorFor(m => m.ScoreCards[i].Marks, new { htmlAttributes = new { @type = "number", @min = 0, id = @Model.ScoreCards[i].Marks} })<br />
}
<input class="btn btn-primary" type="submit" value="Submit" />
}
When I run the application -当我运行应用程序时 -
When I click submit, I am able to see model.FeedBack
but the list is set to null.当我单击提交时,我可以看到model.FeedBack
但列表设置为 null。
Something similar is achieved in this question & it's answer;在这个问题和它的答案中实现了类似的东西; I am not sure what exactly I am missing here.我不确定我在这里缺少什么。
you send no input for the Subject
.您没有为Subject
发送任何输入。 You'll need a hidden input for that along with any other value you want returned to the controller when the form is posted.您需要一个隐藏的输入以及在表单发布时要返回给控制器的任何其他值。 Also a simple text box should work for the marks.一个简单的文本框也应该用于标记。
@using (Html.BeginForm("SubmitScore", "Student", FormMethod.Post))
{
@Html.DisplayName(@Model.StudentId)<br />
@Html.HiddenFor(m => m.StudentId)
<label>Comment:</label><br/>
@Html.EditorFor(m => m.FeedBack, new { htmlAttributes = new { @type = "text", id = @Model.FeedBack} })<br />
for (var i = 0; i < @Model.ScoreCards.Count; i++) {
@Html.HiddenFor(m => m.ScoreCards[i].Subject)
@Html.DisplayName(@Model.ScoreCards[i].Subject) <br/>
@Html.TextBoxFor(m => m.ScoreCards[i].Marks, new { htmlAttributes = new { @type = "number", @min = 0} })<br />
}
<input class="btn btn-primary" type="submit" value="Submit" />
}
Finally you need to use properties in order for the model binding to work.最后,您需要使用属性以使模型绑定工作。 You currently have ScoreCards
as a field so update it to a property.您当前将ScoreCards
作为字段,因此将其更新为属性。
public class StudentViewModel {
public string StudentId { get; set; }
public string FeedBack { get; set; }
public List<ScoreCard> ScoreCards { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.