model是List,提交的model是null
本文关键字:model null List 提交 | 更新日期: 2023-09-27 18:02:57
我需要提交技能列表。当我尝试这样做时提交的模型是空的。但是在Fidler中,我看到数据被发送了。
public class MvcSkill
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Display(Name = "Category Name")]
[StringLength(128, ErrorMessage = "Max length - {0} symbols")]
[Required(ErrorMessage = "Please enter your last name")]
public string Name { get; set; }
public int Level { get; set; }
[Required(ErrorMessage ="Choose skill category")]
public string CategoryName { get; set; }
}
每个技能的部分视图(技能水平的奇怪输入是bootstrap-star-rating:
@using MvcApp.Infrastructure;
@model MvcApp.ViewModels.MvcSkill
<tr>
<td>
@Html.HiddenFor(x => x.Id)
</td>
<td>
@Html.HiddenFor(x => x.Name)
@Html.DisplayFor(x => x.Name)
</td>
<td>
@Html.HiddenFor(x => x.CategoryName)
@Html.DisplayFor(x => x.CategoryName)
</td>
<td>
<input for="Level" id="@Model.Id" value="@Model.Level" name="Level" type="number" class="rating-loading" data-size="xs" data-min="0" data-max="5" data-step="1" data-show-clear="false">
</td>
@{
var identity = (CustomIdentity)User.Identity;
if (identity.Roles.FirstOrDefault(r => r == "Administrator") != null)
{
<td>
@Html.RouteLink("Edit", new { controller = "Administrator", action = "EditSkill", id = Model.Id })
</td>
<td>
@Html.RouteLink("Remove", new { controller = "Administrator", action = "RemoveSkill", id = Model.Id })
</td>
}
}
</tr>
查看技能列表:
@model IList<MvcApp.ViewModels.MvcSkill>
@{
ViewBag.Title = "Skills";
}
@using (Html.BeginForm("Index","Skill", Model))
{
<table>
@foreach (var s in Model)
{
@Html.Partial("_Skill", s)
}
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="submit" type="submit" value="Update" class="btn btn-default" />
</div>
</div>
}
@section scripts{
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet">
<link href="~/Content/star-rating.css" media="all" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
<script src="~/Scripts/star-rating.js" type="text/javascript"></script>
@foreach (var item in Model)
{
string id = item.Id.ToString();
<script>
$(document).on('ready', function () {
$('#@id').rating({
step: 1,
defaultCaption: 'Unknown',
starCaptions: { 0: 'Unknown', 1: 'Beginner', 2: 'Elementary', 3: 'Intermediate', 4: 'Advanced', 5: 'Profi' },
starCaptionClasses: function (val) { return 'text-success'; }
}).on('rating.change', function (event, value, caption) {
Model.Skills.FirstOrDefault(s => s.Key.Id.ToString() == id).Value = value;
});
});
</script>
}
}
和控制器方法:
[HttpPost]
public ActionResult Index(List<MvcSkill> skillModel)
{
//Do something...
return View();
}
费德勒文本:Id = 1,名称= c % 2 b % 2 b& CategoryName = Programing&水平= 5,Id = 3,名字= PHP& CategoryName = Programing&水平= 3,Id = 6,名字= JAVA& CategoryName = Programing&水平= 3,Id = 7,名字= Name& CategoryName = . net + Frameworks&水平= 5
您必须手动添加索引,以便MVC框架能够成功绑定数据。比如:
@using (Html.BeginForm("Index","Skill", Model))
{
<table>
@for (int i = 0; i < Model.Length; i++)
{
<tr>
<td>
@Html.Hidden("[" + i + "].Id");
</td>
<td>
@Html.DisplayFor(m => Model.ElementAt(i).Name)
@Html.Hidden("[" + i + "].Name");
</td>
<td>
@Html.DisplayFor(m => Model.ElementAt(i).CategoryName)
@Html.Hidden("[" + i + "].CategoryName");
</td>
</tr>
}
</table>
}
或者,如注释中所述,您可以使用BeginCollectionItem
(我个人使用)。BeginCollectionItem
所做的是,通常它添加GUIDs作为索引。源代码可以在这里找到,你可以在这里熟悉它