我正在制作我的MVC应用程序。我正在创建一个表格,里面有一个表格。一张桌子应该包含他们拥有的用户和成绩(如学校的标记表)。我希望标记可以修改,所以我使用默认值类型TextBox创建它们,就像在数据库中一样,但显然,当我按下"应用更改"按钮,控制器将所有标记的列表视为空。为什么会这样?
我的观点(其中的一部分):
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@model ClassDeclarationsThsesis.Models.ClassesViewViewModel
@{
ViewBag.Title = "ClassesView";
}
<h2>ClassesView</h2>
<select id="sel">
<option value="week">Current week</option>
<option value="total">Total</option>
</select>
@{
int i = 0;
int inside = 0;
}
@using (Html.BeginForm("ClassesView", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div id="week">
<table border="1" width="500">
<tr>
<th>Surname</th>
<th>Name</th>
@foreach (var task in Model.current)
{
<th>@task.name</th>
}
<th>Total</th>
</tr>
@{
int counter = 0;
int counter1 = 0;
}
@foreach (var user in Model.curr_users)
{
if (user.user_type.Replace(" ", String.Empty) == 1.ToString())
{
<tr>
<th>@user.surname</th>
<th>@user.name</th>
@foreach (var task in Model.current)
{
foreach (var grade in Model.grading)
{
if (user.user_id == grade.user_id)
{
if (task.task_id == grade.task_id)
{
inside = task.value;
i = i + task.value;
counter1++;
}
}
}
<th>@Html.TextBoxFor(m => m.points[counter + counter1 - 1], new { @class = "form-control", @Value = @inside, style = "width:50px;" })</th>
inside = 0;
}
<th>
@i
</th>
@Html.HiddenFor(m => m.class_id)
@Html.HiddenFor(m => m.student_id)
</tr>
}
i = 0;
counter++;
}
</table>
<input type="submit" class="btn btn-default" value="Apply changes" />
</div>
}
我在此处使用的 points
在List<int>
中为ClassesViewViewModel
。我做错了什么?
修改
添加了控制器:
public ActionResult ClassesView(ClassesViewViewModel value)
{
ClassDeclarationsDBEntities4 entities = new ClassDeclarationsDBEntities4();
ClassesViewViewModel model = new ClassesViewViewModel();
model.users = entities.Users.ToList();
model.tasks = entities.Tasks.ToList();
model.group_id = value.group_id;
model.class_id = value.class_id;
model.grading = entities.Gradings.ToList();
model.s_u = entities.Subjects_Users.ToList();
model.t = new List<ClassDeclarationsThsesis.Classes.Task>();
model.current = new List<ClassDeclarationsThsesis.Classes.Task>();
model.curr_users = new List<ClassDeclarationsThsesis.Classes.User>();
DateTime max = DateTime.MinValue;
foreach (var task in model.tasks)
{
if (task.class_id == model.class_id)
{
model.t.Add(new ClassDeclarationsThsesis.Classes.Task(task.task_id, task.class_id, task.name, task.points, task.deadline));
}
}
foreach (var task in model.t)
{
if (DateTime.Compare(task.deadline, max) > 0)
{
max = task.deadline;
}
}
foreach (var task in model.t)
{
if (DateTime.Compare(task.deadline, max) == 0)
{
model.current.Add(task);
}
}
foreach (var s_u in model.s_u)
{
if (s_u.class_id == model.class_id && s_u.group_id == model.group_id)
{
foreach (var user in model.users)
{
if (user.user_id == s_u.user_id)
{
model.curr_users.Add(new ClassDeclarationsThsesis.Classes.User(user.name, user.surname, user.user_id, user.user_type, user.email, user.password));
}
}
}
}
if (ModelState.IsValid)
{
for(int i=0;i<model.points.Count();i++)
{
//getting null here
}
}
else
{
return View(model);
}
return View(model);
}
答案 0 :(得分:1)
1)您应该在HttpPost操作方法中将“值”更改为“模型”。
public ActionResult ClassesView(ClassesViewViewModel model)
2)你的行动方法有很多方面。你应该学习存储库和服务模式。简而言之,你应该将db读取,插入,更新行放在存储库类中。你应该为每个实体创建存储库类。业务逻辑(验证等)必须在服务类中。
答案 1 :(得分:-1)
好的,我找到了答案。我将控制器中的value
更改为model
并更正了计数器以确保值是连续的,从0开始。感谢您的支持。