이것은 약 another question I asked과 (과) 관련된 부분입니다. 사용자 역할 정보를 표에 바인딩하려고하는데 사용자에게 역할을 할당하고 있습니다. 각 사용자는 데이터베이스 내에서 여러 역할을 할 수 있으며 Kendo UI MultiSelect를 사용하여 편집해야합니다.Kendo MultiSelect with Kendo UI Grid with ASP.NET MVC
필요한 역할을 선택하고 컨트롤러에 다시 게시하면 "RoleBasicModel"개체의 배열에 필요한 역할 수가 포함되지만 해당 속성은 모두 비어 있습니다.
모델
는 다음과 같이 정의된다 :@Html.Kendo().MultiSelect().Name("RoleList").DataTextField("Text").DataValueField("Id").BindTo((IEnumerable<Models.RoleBasicModel>)ViewData["uroles"]).Placeholder("No role selected")
:
@(Html.Kendo().Grid<Models.UserInfo>()
.Name("userGrid")
.Columns(columns =>
{
columns.Bound(p => p.UserName);
columns.Bound(p => p.FirstName);
columns.Bound(p => p.LastName);
columns.Bound(p => p.Roles).EditorTemplateName("RoleListEditor").Template(p => p.RoleList);
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.Filterable()
.Sortable()
.Resizable(r => r.Columns(true))
.Editable(editable => { editable.Mode(GridEditMode.InLine); editable.DisplayDeleteConfirmation("Are you sure you want to remove this user?"); })
.HtmlAttributes(new { style = "min-height:90px;max-height:450px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.UserId);
model.Field(p => p.UserId).Editable(false);
model.Field(p => p.FirstName).Editable(true);
model.Field(p => p.LastName).Editable(true);
model.Field(p => p.UserName).Editable(false);
model.Field(p => p.RoleList).Editable(true);
}
).Read(read => read.Action("GetAllUsers", "Admin").Type(HttpVerbs.Get))
.Update(update => update.Action("UpdateUser", "Admin").Type(HttpVerbs.Post))
.Destroy(update => update.Action("DeleteUser", "Admin").Type(HttpVerbs.Post))
)
)
그리고 검도 다중 선택을 사용하여 내 편집기 템플릿, 다음과 같이 정의된다
public class UserInfo
{
public string UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Roles { get; set; }
public IEnumerable<RoleBasicModel> RoleList { get; set; }
}
public class RoleBasicModel
{
public string Id { get; set; }
public string Text { get; set; }
}
그리드는 설정과 같다
서버로 다시 전송 된 데이터가 비어있는 이유가 있습니까? 사용할 올바른 모델을 정의 할 MultiSelect 컨트롤에서 뭔가 빠졌다고 생각합니다. 나는 종종 비슷한 질문에 대한 답으로 인용되는 the test project을 언급했지만 그와도 아무런 기쁨도 얻지 못했습니다. 요청한 바와 같이
(의 요약본) 내가 사용 컨트롤러 :
public ActionResult ManageUsers()
{
PopulateRoles();
return View();
}
private void PopulateRoles()
{
ViewData["uroles"] = new ApplicationDbContext().Roles.Select(r => new RoleBasicModel { Text = r.Name, Id = r.Id }).ToList();
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetAllUsers([DataSourceRequest]DataSourceRequest request)
{
using (var context = new ApplicationDbContext())
{
var allUsers = context.Users.ToList().Select(x =>
new UserInfo
{
UserName = x.UserName,
UserId = x.Id,
FirstName = x.FirstName,
LastName = x.LastName,
RoleList = x.Roles.Select(p => new RoleBasicModel { Text = p.Role.Name, Id = p.RoleId }),
Roles = string.Join(", ", x.Roles.Select(p => p.Role.Name).ToList())
}).ToList();
return Json(allUsers.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateUser([DataSourceRequest] DataSourceRequest request, UserInfo user)
{
if (user != null && ModelState.IsValid)
{
using (var context = new ApplicationDbContext())
{
// Do something with the user details
}
}
return Json(new[] { user }.ToDataSourceResult(request, ModelState));
}
EDIT : 재생 서버에 등록한 데이터를 시청하면 선택된 개체들의 어레이 외설 것처럼, 그것은 '표시 t는 올바르게 파싱되었습니다. 형식은 RoleList [0] .Id : 123456이어야하며 대신 RoleList [0] [Id] : 123456입니다. 이 코드는 MultiSelect 컨트롤에서 문제가 될 수 있다고 생각합니다.
(당신이 검도 그리드를 사용하는 경우) 서버에 게시하기 전에 검도 다중 선택 자신 내부의 데이터에는 직렬화 ? 우리 멍청이를 줘!:) – loxdog
나는 당신에게 똑같은 질문을 던질 수있다. 분명히 완성되지 않은 것을 게시하는 이유는 무엇인가? – tnw
우연히 enter! 이전에 Stack에서 만들었던 정직한 실수 ... – loxdog