【发布时间】:2017-05-12 05:50:42
【问题描述】:
我正在做我的 MVC 应用程序。我有一个从另一个视图获取数据的视图。此视图是要填写的表格。
public ActionResult AddGroupsQty(int qty, int id)
{
var model = new AddGroupsQtyViewModel();
model.subject_id = id;
model.qty = 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 < 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(id, maxId+1, model.group_names[i], teacher.user_id);
}
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", 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>
@Html.TextBoxFor(m => m.group_names[i], new { @class = "form-control" })
</th>
<th>
@Html.TextBoxFor(m => m.teacher_emails[i], new { @class = "form-control" })
</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>
}
}
它所做的或多或少是需要 qty 并为这么多行生成一个表。然后,这些行有要填写的文本框,它应该代表我的视图模型中的数据。但是,当我提交时,我收到这样的错误:
参数字典包含参数“数量”的空条目 方法的不可空类型“System.Int32” 'System.Web.Mvc.ActionResult AddGroupsQty(Int32, Int32)' 在 'ClassDeclarationsThsesis.Controllers.AccountController'。一个可选的 参数必须是引用类型、可为空的类型或声明为 一个可选参数。 Nazwa 参数:参数
我该如何解决这个问题?
【问题讨论】:
-
我不太了解您要做什么,但您遇到的错误是因为您指向了一个需要发送两个参数的操作“AddGroupsQty”:int qty, int id,并且两者都没有被发送,因此它无法路由到引发该异常的那个动作。问候。
-
但是窗口打开了,因为它获取了所需的参数。但是,在视图中,当我单击提交(查看视图)时,它崩溃了。 @dime2lo
-
您还有其他与您发布的操作同名的操作吗?如果问题中的那个没有上面的http属性(HttpGet或HttpPost)?
-
没有@dime2lo
-
我相信你试图用那个动作做太多。只要我理解该操作被用于呈现初始状态(我认为它是工作状态)并且它已经被用于尝试修改已经显示的数据(这不起作用)。但是,正如我之前所说,您得到的异常是因为在提交表单时没有提供参数。您想进行测试(因此异常停止发生),您可以将缺少的参数添加到
@Html.BeginForm或将它们放在表单的正文中。
标签: c# .net asp.net-mvc asp.net-mvc-4 model-view-controller