기술 목록을 제출해야합니다. 내가 제출하려고 할 때이 모델은 null입니다. 그러나 Fidler에서는 데이터가 전송 된 것을 볼 수 있습니다.mvc 5, 모델이 List이고, 제출 된 모델이 null입니다.
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; }
}
각 능력 (스킬 레벨 이상 입력
부분보기는 부트 스트랩 - 별 평가입니다 : 기술의 목록
@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();
}
피들러 텍스트 : 식 1 & NAME = C %의 2B % 2B & 범주 =는 프로그래밍 & 레벨 = 5 & 식 3 & NAME = PHP & 범주 =는 프로그래밍 & 레벨 = 3 & 식 6 & NAME = JAVA & 범주 =는 프로그래밍 & 수준 = 3 & 아이디 = 7 & 이름 = 이름 & 범주 = 닷넷 +는 & 레벨 프레임 워크 = 5
[가변 길이 목록 편집, ASP.NET MVC 2 스타일] (http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list)에서 살펴볼 수 있습니다. -aspnet-mvc-2-style /). 그는 NuGet [BeginCollectionItem HtmlHelper] (https://www.nuget.org/packages/BeginCollectionItem/)을 사용합니다. 가변 길이 목록을 다른 MVC 컨트롤러 작업에 게시 할 수 있도록 설계되었습니다. –
'foreach' 루프를 사용하여 컬렉션에 대한 폼 컨트롤을 생성 할 수 없습니다 ([this answer] (http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) - typeof'MvcSkill'에 대한'EditorTemplate'의'for' 루프가 필요합니다.) –