0
MVCアプリケーションを作成しています。私は記入するフォームのビューを持っていますが、それはコントローラが入力されたデータを取得しないようです(デバッグ時にはnullです)。
マイコントローラ:コントローラに渡すMVC動的データ
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);
}
マイビューモデル:
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; }
}
とビュー:
@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>
}
}
問題であり、アプリケーションを実行して、フォームを記入し、押して提出するときにボタンを押すと、コントローラーにnull例外がスローされ、両方ともリストされます。
と:リストteacher_emails
一覧は
がnullのGROUP_NAMES。私は私のフォームにエラーが表示されません。どうすれば解決できますか?