Map nested models in asp.net core views
up vote
1
down vote
favorite
I want to create roles with respective claims in asp.net core 2+, so I create a view like:
@page
@model Security.Dto.Models.ApplicationRoleModel
<form asp-controller="security" asp-action="CreateRole" method="post">
<h4>Create new Role</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group" id="item-list">
<label>Role Name</label>
<input asp-for="RoleClaimList[0].Role.Name" class="form-control roles" />
</div>
<a href="#" id="add">Add another</a>
<div class="form-group" id="claim-list">
<label>Claim Type</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Type" class="form-control " />
<label>Claim Value</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Value" class="form-control claims" />
</div>
<br />
<button type="submit" class="btn btn-default">Create</button>
</form>
@section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".roles").length);
var n = '<label>Role Name</label><input class="form-control roles" name="RoleClaimList[' + i + '].Role.Name" />'
$("#item-list").append(n);
});
});
</script>
}
Controller:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
await _roleManager.AddClaimAsync(item.Role, claim);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
ApplicationRoleModel:
public class ApplicationRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
RoleClaimModel:
public class ClaimsToRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<Claim> ClaimList { get; set; }
}
So when I run and debbug it, Role
come correctly but ClaimList
always count 0 or come null
Someone looks what am I doing wrong? How can I map ClaimList
property model correctly from view?
Regards
c# asp.net asp.net-web-api asp.net-core .net-core
add a comment |
up vote
1
down vote
favorite
I want to create roles with respective claims in asp.net core 2+, so I create a view like:
@page
@model Security.Dto.Models.ApplicationRoleModel
<form asp-controller="security" asp-action="CreateRole" method="post">
<h4>Create new Role</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group" id="item-list">
<label>Role Name</label>
<input asp-for="RoleClaimList[0].Role.Name" class="form-control roles" />
</div>
<a href="#" id="add">Add another</a>
<div class="form-group" id="claim-list">
<label>Claim Type</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Type" class="form-control " />
<label>Claim Value</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Value" class="form-control claims" />
</div>
<br />
<button type="submit" class="btn btn-default">Create</button>
</form>
@section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".roles").length);
var n = '<label>Role Name</label><input class="form-control roles" name="RoleClaimList[' + i + '].Role.Name" />'
$("#item-list").append(n);
});
});
</script>
}
Controller:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
await _roleManager.AddClaimAsync(item.Role, claim);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
ApplicationRoleModel:
public class ApplicationRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
RoleClaimModel:
public class ClaimsToRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<Claim> ClaimList { get; set; }
}
So when I run and debbug it, Role
come correctly but ClaimList
always count 0 or come null
Someone looks what am I doing wrong? How can I map ClaimList
property model correctly from view?
Regards
c# asp.net asp.net-web-api asp.net-core .net-core
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to create roles with respective claims in asp.net core 2+, so I create a view like:
@page
@model Security.Dto.Models.ApplicationRoleModel
<form asp-controller="security" asp-action="CreateRole" method="post">
<h4>Create new Role</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group" id="item-list">
<label>Role Name</label>
<input asp-for="RoleClaimList[0].Role.Name" class="form-control roles" />
</div>
<a href="#" id="add">Add another</a>
<div class="form-group" id="claim-list">
<label>Claim Type</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Type" class="form-control " />
<label>Claim Value</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Value" class="form-control claims" />
</div>
<br />
<button type="submit" class="btn btn-default">Create</button>
</form>
@section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".roles").length);
var n = '<label>Role Name</label><input class="form-control roles" name="RoleClaimList[' + i + '].Role.Name" />'
$("#item-list").append(n);
});
});
</script>
}
Controller:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
await _roleManager.AddClaimAsync(item.Role, claim);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
ApplicationRoleModel:
public class ApplicationRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
RoleClaimModel:
public class ClaimsToRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<Claim> ClaimList { get; set; }
}
So when I run and debbug it, Role
come correctly but ClaimList
always count 0 or come null
Someone looks what am I doing wrong? How can I map ClaimList
property model correctly from view?
Regards
c# asp.net asp.net-web-api asp.net-core .net-core
I want to create roles with respective claims in asp.net core 2+, so I create a view like:
@page
@model Security.Dto.Models.ApplicationRoleModel
<form asp-controller="security" asp-action="CreateRole" method="post">
<h4>Create new Role</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group" id="item-list">
<label>Role Name</label>
<input asp-for="RoleClaimList[0].Role.Name" class="form-control roles" />
</div>
<a href="#" id="add">Add another</a>
<div class="form-group" id="claim-list">
<label>Claim Type</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Type" class="form-control " />
<label>Claim Value</label>
<input asp-for="RoleClaimList[0].ClaimList[0].Value" class="form-control claims" />
</div>
<br />
<button type="submit" class="btn btn-default">Create</button>
</form>
@section Scripts {
<script>
$(function () {
$("#add").click(function (e) {
e.preventDefault();
var i = ($(".roles").length);
var n = '<label>Role Name</label><input class="form-control roles" name="RoleClaimList[' + i + '].Role.Name" />'
$("#item-list").append(n);
});
});
</script>
}
Controller:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
await _roleManager.AddClaimAsync(item.Role, claim);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
ApplicationRoleModel:
public class ApplicationRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
RoleClaimModel:
public class ClaimsToRoleModel
{
public List<RoleClaimModel> RoleClaimList { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<Claim> ClaimList { get; set; }
}
So when I run and debbug it, Role
come correctly but ClaimList
always count 0 or come null
Someone looks what am I doing wrong? How can I map ClaimList
property model correctly from view?
Regards
c# asp.net asp.net-web-api asp.net-core .net-core
c# asp.net asp.net-web-api asp.net-core .net-core
edited Nov 9 at 22:15
asked Nov 9 at 19:16
Jonathan
1355
1355
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your problem is caused because the Claim
class doesn't have a constructor that takes 0 parameters so when the framework attempts to create a new one it cannot, so you end up with an empty or null object.
To solve the issue you need to use your own custom class in your hierarchy:
public class CustomClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<CustomClaim> ClaimList { get; set; }
}
Now the framework can create the CustomClaim
and serialize it to your API. In your controller you can translate your CustomClaim
into the .NET Claim
and pass that to your _roleManager
:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
var c = new System.Security.Claims.Claim(claim.Type, claim.Value);
await _roleManager.AddClaimAsync(item.Role, c);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232005%2fmap-nested-models-in-asp-net-core-views%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your problem is caused because the Claim
class doesn't have a constructor that takes 0 parameters so when the framework attempts to create a new one it cannot, so you end up with an empty or null object.
To solve the issue you need to use your own custom class in your hierarchy:
public class CustomClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<CustomClaim> ClaimList { get; set; }
}
Now the framework can create the CustomClaim
and serialize it to your API. In your controller you can translate your CustomClaim
into the .NET Claim
and pass that to your _roleManager
:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
var c = new System.Security.Claims.Claim(claim.Type, claim.Value);
await _roleManager.AddClaimAsync(item.Role, c);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
add a comment |
up vote
1
down vote
accepted
Your problem is caused because the Claim
class doesn't have a constructor that takes 0 parameters so when the framework attempts to create a new one it cannot, so you end up with an empty or null object.
To solve the issue you need to use your own custom class in your hierarchy:
public class CustomClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<CustomClaim> ClaimList { get; set; }
}
Now the framework can create the CustomClaim
and serialize it to your API. In your controller you can translate your CustomClaim
into the .NET Claim
and pass that to your _roleManager
:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
var c = new System.Security.Claims.Claim(claim.Type, claim.Value);
await _roleManager.AddClaimAsync(item.Role, c);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your problem is caused because the Claim
class doesn't have a constructor that takes 0 parameters so when the framework attempts to create a new one it cannot, so you end up with an empty or null object.
To solve the issue you need to use your own custom class in your hierarchy:
public class CustomClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<CustomClaim> ClaimList { get; set; }
}
Now the framework can create the CustomClaim
and serialize it to your API. In your controller you can translate your CustomClaim
into the .NET Claim
and pass that to your _roleManager
:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
var c = new System.Security.Claims.Claim(claim.Type, claim.Value);
await _roleManager.AddClaimAsync(item.Role, c);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
Your problem is caused because the Claim
class doesn't have a constructor that takes 0 parameters so when the framework attempts to create a new one it cannot, so you end up with an empty or null object.
To solve the issue you need to use your own custom class in your hierarchy:
public class CustomClaim
{
public string Type { get; set; }
public string Value { get; set; }
}
public class RoleClaimModel
{
public ApplicationRole Role { get; set; }
public List<CustomClaim> ClaimList { get; set; }
}
Now the framework can create the CustomClaim
and serialize it to your API. In your controller you can translate your CustomClaim
into the .NET Claim
and pass that to your _roleManager
:
public async Task<IActionResult> CreateRole(ApplicationRoleModel model)
{
try
{
foreach (var item in model.RoleClaimList)
{
var roleExists = await _roleManager.RoleExistsAsync(item.Role.Name);
if (roleExists) continue;
var createRole = _roleManager.CreateAsync(item.Role);
foreach (var claim in item.ClaimList)
{
var c = new System.Security.Claims.Claim(claim.Type, claim.Value);
await _roleManager.AddClaimAsync(item.Role, c);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
answered Nov 10 at 1:58
Simply Ged
2,0782921
2,0782921
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232005%2fmap-nested-models-in-asp-net-core-views%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown