[英]MVC dynamic data passing to controller
I am creating a MVC application. 我正在创建一个MVC应用程序。 I am having a view with a form to fill, but it seems that controller does not get data that was entered (it is null when debugging).
我有一个要填写表格的视图,但是控制器似乎无法获取输入的数据(调试时为null)。
My controller: 我的控制器:
public ActionResult AddGroupsQty(AddGroupsQtyViewModel value)
{
var model = new AddGroupsQtyViewModel();
model.subject_id = value.subject_id;
model.qty = value.qty;
ClassDeclarationsDBEntities1 entities1=new ClassDeclarationsDBEntities1();
var subj = entities1.Subjects
.Where(b => b.class_id == model.subject_id)
.FirstOrDefault();
model.subject_name = subj.name;
if (ModelState.IsValid)
{
int maxId = 0;
int total = 0;
total = entities1.Groups.Count();
if (total == 0)
{
maxId = 0;
}
else
{
maxId = entities1.Groups.Max(u => u.group_id);
}
for (int i = 0; i < value.qty; i++)
{
var teacher = entities1.Users
.Where(b => b.email.Replace(" ", String.Empty) == model.teacher_emails[i].Replace(" ", String.Empty))
.FirstOrDefault();
var group=new Models.Group(value.subject_id, maxId+1, model.group_names[i], teacher.user_id);
entities1.Groups.Add(group);
entities1.SaveChangesAsync();
}
return RedirectToAction("OperationSuccess", "Account");
}
return View(model);
}
My View model: 我的视图模型:
public class AddGroupsQtyViewModel
{
public int qty { get; set; }
public int subject_id { get; set; }
public string subject_name { get; set; }
[Required]
[Display(Name = "Name of group")]
public List<string> group_names { get; set; }
[Required]
[Display(Name = "Email of teacher")]
public List<string> teacher_emails { get; set; }
}
and the View: 和视图:
@using System.IdentityModel.Configuration
@using System.Web.UI.WebControls
@model ClassDeclarationsThsesis.Models.AddGroupsQtyViewModel
@{
ViewBag.Title = "AddGroupsQty";
}
<h2>Add Groups to @Model.subject_name</h2>
@if (Model != null)
{
using (Html.BeginForm("AddGroupsQty", "Account", new { qty = Model.qty, Model.subject_id }, FormMethod.Post, new { @class = "form-horizontal", role = "form", }))
{
@Html.AntiForgeryToken()
<h4>Insert data</h4>
<hr />
<table>
<tr>
<th>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.group_names, new { @class = "col-md-2 control-label" })
</div>
</th>
<th>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.teacher_emails, new { @class = "col-md-2 control-label" })
</div>
</th>
</tr>
@for (int i = 0; i < Model.qty; i++)
{
<tr>
<th>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" })
</div>
</div>
</th>
<th>
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" })
</div>
</div>
</th>
</tr>
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Submit" />
</div>
</div>
}
}
The problem is that when running the application, filling the form and submitting by pressing the button, it throws a null exception in controller that both lists: 问题在于,当运行应用程序,填写表单并通过按下按钮提交时,它将在控制器中引发一个空异常,该异常同时列出:
List teacher_emails
列出Teacher_emails
and: 和:
List group_names
列出组名
are null. 为空。 I do not see any error in my form.
我的表格没有看到任何错误。 How can I solve it?
我该如何解决?
model.teacher_emails and model.group_names are always null, because model is new. model.teacher_emails和model.group_names始终为null,因为model是新的。
var model = new AddGroupsQtyViewModel();
Try using value.teacher_emails
and value.group_names
instead. 尝试改用
value.teacher_emails
和value.group_names
。
Also tidy that code up. 也整理一下代码。 A blank line after the variable declaration, and you would have spotted it yourself.
变量声明后的空白行,您自己会发现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.