When creating double dropdown SelectList with JQuery on POST returns 0. ASP.Net CORE 2.1











up vote
0
down vote

favorite












I have created two dropdownlists, of which one acts as a filter for the other. So, with dropdown Customer a customer is selected and only a limited set of ClientUsers is presented in in the dropdown ClientUser. I use a Jquery function to make this happen.



The selection works excellent, but when I POST the form the ClientUser is set to 0, instead of the choice.



enter image description here



The View is as follows (simplified for readability purposes):



@model DropDownTester2.Models.CaseViewModel

<form asp-action="Maak">
<label asp-for="CaseTitle" class="control-label"></label>
<input asp-for="CaseTitle" class="form-control" />

<select id="customerId" asp-for="CustomerId" asp-items='@(new SelectList(ViewBag.customers, "CustomerId", "CompanyName"))'>
<option>Select Customer Name</option>
</select>

<select id="clientUserId" asp-for="ClientUserId" asp-items='@(new SelectList(string.Empty, "ClientUserId", "LastName"))'>
</select>

<input type="submit" value="Maak" class="btn btn-default" />
</form>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("#customerId").change(function () {

var url = '@Url.Content("~/")' + "Case/getClientUserById";

var ddlsource = "#customerId";

$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
//$.getJSON("@Url.Action("getClientUserById","Case")", { id: $(ddlsource).val() }, function (data) {
var items = '';
$("#clientUserId").empty();
$.each(data, function (i, row) {
items += "<option value='" + row.value + "'>" + row.text + "</option>";
});
$("#clientUserId").html(items);

})
});
});

</script>
}


The CaseViewModel is:



public class CaseViewModel
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public IEnumerable<Customer> CustomerList { get; set; }
public IEnumerable<ClientUser> ClientUserList {get; set;}


My Model for Case is:



public class Case
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public string CaseRoleDescription { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public virtual Customer Customer { get; set; }
public virtual ICollection<ClientUser> ClientUsers { get; set; }

}


Finally my controllers are :



// GET: Case/Maak
public IActionResult Maak()
{
ViewBag.customers = _context.Customer.ToList();
return View();
}

public JsonResult getClientUserById(int id)
{
List<ClientUser> list = new List<ClientUser>();
list = _context.ClientUser.Where(c => c.Customer.CustomerId == id).ToList();
list.Insert(0, new ClientUser { ClientUserId = 0, LastName = "Please select a clientuser" });
return Json(new SelectList(list, "ClientUserId", "LastName"));
}


// POST: Case/Maak
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Maak(CaseViewModel model)
{
if (ModelState.IsValid)
{
var newCase = new Case
{
CaseId = model.CaseId,
CaseTitle = model.CaseTitle,
CustomerId = model.CustomerId,
ClientUserId = model.ClientUserId
};

_context.Add(newCase);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CustomerId"] = new SelectList(_context.Set<Customer>(), "CustomerId", "CustomerId", model.CustomerId);
return View(model);
}









share|improve this question






















  • the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
    – Foo
    Nov 19 at 14:38















up vote
0
down vote

favorite












I have created two dropdownlists, of which one acts as a filter for the other. So, with dropdown Customer a customer is selected and only a limited set of ClientUsers is presented in in the dropdown ClientUser. I use a Jquery function to make this happen.



The selection works excellent, but when I POST the form the ClientUser is set to 0, instead of the choice.



enter image description here



The View is as follows (simplified for readability purposes):



@model DropDownTester2.Models.CaseViewModel

<form asp-action="Maak">
<label asp-for="CaseTitle" class="control-label"></label>
<input asp-for="CaseTitle" class="form-control" />

<select id="customerId" asp-for="CustomerId" asp-items='@(new SelectList(ViewBag.customers, "CustomerId", "CompanyName"))'>
<option>Select Customer Name</option>
</select>

<select id="clientUserId" asp-for="ClientUserId" asp-items='@(new SelectList(string.Empty, "ClientUserId", "LastName"))'>
</select>

<input type="submit" value="Maak" class="btn btn-default" />
</form>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("#customerId").change(function () {

var url = '@Url.Content("~/")' + "Case/getClientUserById";

var ddlsource = "#customerId";

$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
//$.getJSON("@Url.Action("getClientUserById","Case")", { id: $(ddlsource).val() }, function (data) {
var items = '';
$("#clientUserId").empty();
$.each(data, function (i, row) {
items += "<option value='" + row.value + "'>" + row.text + "</option>";
});
$("#clientUserId").html(items);

})
});
});

</script>
}


The CaseViewModel is:



public class CaseViewModel
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public IEnumerable<Customer> CustomerList { get; set; }
public IEnumerable<ClientUser> ClientUserList {get; set;}


My Model for Case is:



public class Case
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public string CaseRoleDescription { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public virtual Customer Customer { get; set; }
public virtual ICollection<ClientUser> ClientUsers { get; set; }

}


Finally my controllers are :



// GET: Case/Maak
public IActionResult Maak()
{
ViewBag.customers = _context.Customer.ToList();
return View();
}

public JsonResult getClientUserById(int id)
{
List<ClientUser> list = new List<ClientUser>();
list = _context.ClientUser.Where(c => c.Customer.CustomerId == id).ToList();
list.Insert(0, new ClientUser { ClientUserId = 0, LastName = "Please select a clientuser" });
return Json(new SelectList(list, "ClientUserId", "LastName"));
}


// POST: Case/Maak
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Maak(CaseViewModel model)
{
if (ModelState.IsValid)
{
var newCase = new Case
{
CaseId = model.CaseId,
CaseTitle = model.CaseTitle,
CustomerId = model.CustomerId,
ClientUserId = model.ClientUserId
};

_context.Add(newCase);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CustomerId"] = new SelectList(_context.Set<Customer>(), "CustomerId", "CustomerId", model.CustomerId);
return View(model);
}









share|improve this question






















  • the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
    – Foo
    Nov 19 at 14:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have created two dropdownlists, of which one acts as a filter for the other. So, with dropdown Customer a customer is selected and only a limited set of ClientUsers is presented in in the dropdown ClientUser. I use a Jquery function to make this happen.



The selection works excellent, but when I POST the form the ClientUser is set to 0, instead of the choice.



enter image description here



The View is as follows (simplified for readability purposes):



@model DropDownTester2.Models.CaseViewModel

<form asp-action="Maak">
<label asp-for="CaseTitle" class="control-label"></label>
<input asp-for="CaseTitle" class="form-control" />

<select id="customerId" asp-for="CustomerId" asp-items='@(new SelectList(ViewBag.customers, "CustomerId", "CompanyName"))'>
<option>Select Customer Name</option>
</select>

<select id="clientUserId" asp-for="ClientUserId" asp-items='@(new SelectList(string.Empty, "ClientUserId", "LastName"))'>
</select>

<input type="submit" value="Maak" class="btn btn-default" />
</form>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("#customerId").change(function () {

var url = '@Url.Content("~/")' + "Case/getClientUserById";

var ddlsource = "#customerId";

$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
//$.getJSON("@Url.Action("getClientUserById","Case")", { id: $(ddlsource).val() }, function (data) {
var items = '';
$("#clientUserId").empty();
$.each(data, function (i, row) {
items += "<option value='" + row.value + "'>" + row.text + "</option>";
});
$("#clientUserId").html(items);

})
});
});

</script>
}


The CaseViewModel is:



public class CaseViewModel
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public IEnumerable<Customer> CustomerList { get; set; }
public IEnumerable<ClientUser> ClientUserList {get; set;}


My Model for Case is:



public class Case
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public string CaseRoleDescription { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public virtual Customer Customer { get; set; }
public virtual ICollection<ClientUser> ClientUsers { get; set; }

}


Finally my controllers are :



// GET: Case/Maak
public IActionResult Maak()
{
ViewBag.customers = _context.Customer.ToList();
return View();
}

public JsonResult getClientUserById(int id)
{
List<ClientUser> list = new List<ClientUser>();
list = _context.ClientUser.Where(c => c.Customer.CustomerId == id).ToList();
list.Insert(0, new ClientUser { ClientUserId = 0, LastName = "Please select a clientuser" });
return Json(new SelectList(list, "ClientUserId", "LastName"));
}


// POST: Case/Maak
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Maak(CaseViewModel model)
{
if (ModelState.IsValid)
{
var newCase = new Case
{
CaseId = model.CaseId,
CaseTitle = model.CaseTitle,
CustomerId = model.CustomerId,
ClientUserId = model.ClientUserId
};

_context.Add(newCase);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CustomerId"] = new SelectList(_context.Set<Customer>(), "CustomerId", "CustomerId", model.CustomerId);
return View(model);
}









share|improve this question













I have created two dropdownlists, of which one acts as a filter for the other. So, with dropdown Customer a customer is selected and only a limited set of ClientUsers is presented in in the dropdown ClientUser. I use a Jquery function to make this happen.



The selection works excellent, but when I POST the form the ClientUser is set to 0, instead of the choice.



enter image description here



The View is as follows (simplified for readability purposes):



@model DropDownTester2.Models.CaseViewModel

<form asp-action="Maak">
<label asp-for="CaseTitle" class="control-label"></label>
<input asp-for="CaseTitle" class="form-control" />

<select id="customerId" asp-for="CustomerId" asp-items='@(new SelectList(ViewBag.customers, "CustomerId", "CompanyName"))'>
<option>Select Customer Name</option>
</select>

<select id="clientUserId" asp-for="ClientUserId" asp-items='@(new SelectList(string.Empty, "ClientUserId", "LastName"))'>
</select>

<input type="submit" value="Maak" class="btn btn-default" />
</form>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("#customerId").change(function () {

var url = '@Url.Content("~/")' + "Case/getClientUserById";

var ddlsource = "#customerId";

$.getJSON(url, { id: $(ddlsource).val() }, function (data) {
//$.getJSON("@Url.Action("getClientUserById","Case")", { id: $(ddlsource).val() }, function (data) {
var items = '';
$("#clientUserId").empty();
$.each(data, function (i, row) {
items += "<option value='" + row.value + "'>" + row.text + "</option>";
});
$("#clientUserId").html(items);

})
});
});

</script>
}


The CaseViewModel is:



public class CaseViewModel
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public IEnumerable<Customer> CustomerList { get; set; }
public IEnumerable<ClientUser> ClientUserList {get; set;}


My Model for Case is:



public class Case
{
public int CaseId { get; set; }
public string CaseTitle { get; set; }
public string CaseRoleDescription { get; set; }
public int CustomerId { get; set; }
public int ClientUserId { get; set; }

public virtual Customer Customer { get; set; }
public virtual ICollection<ClientUser> ClientUsers { get; set; }

}


Finally my controllers are :



// GET: Case/Maak
public IActionResult Maak()
{
ViewBag.customers = _context.Customer.ToList();
return View();
}

public JsonResult getClientUserById(int id)
{
List<ClientUser> list = new List<ClientUser>();
list = _context.ClientUser.Where(c => c.Customer.CustomerId == id).ToList();
list.Insert(0, new ClientUser { ClientUserId = 0, LastName = "Please select a clientuser" });
return Json(new SelectList(list, "ClientUserId", "LastName"));
}


// POST: Case/Maak
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Maak(CaseViewModel model)
{
if (ModelState.IsValid)
{
var newCase = new Case
{
CaseId = model.CaseId,
CaseTitle = model.CaseTitle,
CustomerId = model.CustomerId,
ClientUserId = model.ClientUserId
};

_context.Add(newCase);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CustomerId"] = new SelectList(_context.Set<Customer>(), "CustomerId", "CustomerId", model.CustomerId);
return View(model);
}






javascript jquery asp.net asp.net-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 14:25









Charles de M.

479




479












  • the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
    – Foo
    Nov 19 at 14:38


















  • the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
    – Foo
    Nov 19 at 14:38
















the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
– Foo
Nov 19 at 14:38




the ClientUser is set to 0, instead of the choice I think it was ClientUserList instead of ClientUser
– Foo
Nov 19 at 14:38












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I can't reproduce your issue :



enter image description here



The difference part of codes is that i create the Customer/ClientUser manually but that shouldn't be the issue cause :



    public class Customer {
public int CustomerId { get; set; }

public string CompanyName { get; set; }
}

public class ClientUser
{
public int ClientUserId { get; set; }

public string LastName { get; set; }
}


You may search the "ClientUserId " in your pages to confirm whether other function reset the value .



As a workaround, you can also :




  1. Create a hidden filed in your page and bind to your model property , create the select change event javascript function to set the value based on the selection .

  2. Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .






share|improve this answer





















  • Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
    – Charles de M.
    Nov 20 at 17:16










  • @CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
    – Nan Yu
    Nov 22 at 8:22










  • Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
    – Charles de M.
    Nov 22 at 8:35












  • Would you have a ScreenPrint of the ViewPage you created?
    – Charles de M.
    Nov 22 at 10:21










  • Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
    – Charles de M.
    Nov 22 at 11:27











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376693%2fwhen-creating-double-dropdown-selectlist-with-jquery-on-post-returns-0-asp-net%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
0
down vote













I can't reproduce your issue :



enter image description here



The difference part of codes is that i create the Customer/ClientUser manually but that shouldn't be the issue cause :



    public class Customer {
public int CustomerId { get; set; }

public string CompanyName { get; set; }
}

public class ClientUser
{
public int ClientUserId { get; set; }

public string LastName { get; set; }
}


You may search the "ClientUserId " in your pages to confirm whether other function reset the value .



As a workaround, you can also :




  1. Create a hidden filed in your page and bind to your model property , create the select change event javascript function to set the value based on the selection .

  2. Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .






share|improve this answer





















  • Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
    – Charles de M.
    Nov 20 at 17:16










  • @CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
    – Nan Yu
    Nov 22 at 8:22










  • Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
    – Charles de M.
    Nov 22 at 8:35












  • Would you have a ScreenPrint of the ViewPage you created?
    – Charles de M.
    Nov 22 at 10:21










  • Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
    – Charles de M.
    Nov 22 at 11:27















up vote
0
down vote













I can't reproduce your issue :



enter image description here



The difference part of codes is that i create the Customer/ClientUser manually but that shouldn't be the issue cause :



    public class Customer {
public int CustomerId { get; set; }

public string CompanyName { get; set; }
}

public class ClientUser
{
public int ClientUserId { get; set; }

public string LastName { get; set; }
}


You may search the "ClientUserId " in your pages to confirm whether other function reset the value .



As a workaround, you can also :




  1. Create a hidden filed in your page and bind to your model property , create the select change event javascript function to set the value based on the selection .

  2. Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .






share|improve this answer





















  • Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
    – Charles de M.
    Nov 20 at 17:16










  • @CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
    – Nan Yu
    Nov 22 at 8:22










  • Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
    – Charles de M.
    Nov 22 at 8:35












  • Would you have a ScreenPrint of the ViewPage you created?
    – Charles de M.
    Nov 22 at 10:21










  • Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
    – Charles de M.
    Nov 22 at 11:27













up vote
0
down vote










up vote
0
down vote









I can't reproduce your issue :



enter image description here



The difference part of codes is that i create the Customer/ClientUser manually but that shouldn't be the issue cause :



    public class Customer {
public int CustomerId { get; set; }

public string CompanyName { get; set; }
}

public class ClientUser
{
public int ClientUserId { get; set; }

public string LastName { get; set; }
}


You may search the "ClientUserId " in your pages to confirm whether other function reset the value .



As a workaround, you can also :




  1. Create a hidden filed in your page and bind to your model property , create the select change event javascript function to set the value based on the selection .

  2. Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .






share|improve this answer












I can't reproduce your issue :



enter image description here



The difference part of codes is that i create the Customer/ClientUser manually but that shouldn't be the issue cause :



    public class Customer {
public int CustomerId { get; set; }

public string CompanyName { get; set; }
}

public class ClientUser
{
public int ClientUserId { get; set; }

public string LastName { get; set; }
}


You may search the "ClientUserId " in your pages to confirm whether other function reset the value .



As a workaround, you can also :




  1. Create a hidden filed in your page and bind to your model property , create the select change event javascript function to set the value based on the selection .

  2. Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 5:55









Nan Yu

5,9052648




5,9052648












  • Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
    – Charles de M.
    Nov 20 at 17:16










  • @CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
    – Nan Yu
    Nov 22 at 8:22










  • Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
    – Charles de M.
    Nov 22 at 8:35












  • Would you have a ScreenPrint of the ViewPage you created?
    – Charles de M.
    Nov 22 at 10:21










  • Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
    – Charles de M.
    Nov 22 at 11:27


















  • Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
    – Charles de M.
    Nov 20 at 17:16










  • @CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
    – Nan Yu
    Nov 22 at 8:22










  • Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
    – Charles de M.
    Nov 22 at 8:35












  • Would you have a ScreenPrint of the ViewPage you created?
    – Charles de M.
    Nov 22 at 10:21










  • Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
    – Charles de M.
    Nov 22 at 11:27
















Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
– Charles de M.
Nov 20 at 17:16




Hi Nan Yu, thank you for your advice. Will try. I have recreated the solution (this time with Country/State example), but exactly the same issue. I get the impression the problem may be on how asp-items handles the code. Perhaps ASP.Net 2.1 does it slightly different. I will most certainly try the Ajax suggestion. As I am not very good at that, would you have an example?
– Charles de M.
Nov 20 at 17:16












@CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
– Nan Yu
Nov 22 at 8:22




@CharlesdeM. The same as your ajax to call getClientUserById function , in the js function you need to get the selected value using JQuery .
– Nan Yu
Nov 22 at 8:22












Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
– Charles de M.
Nov 22 at 8:35






Thank you. I have found the core of the error: If I create a SelectList in the Controller instead of on the page and have asp-items just use it as a ViewBag it works fine. so in the controller have ViewBag.countries = new SelectList(_context.Customer, "Id", "Name"); and in the View use asp-items="@ViewBag.Countries". If I could replicate that in the JQuery function, I would be saved.
– Charles de M.
Nov 22 at 8:35














Would you have a ScreenPrint of the ViewPage you created?
– Charles de M.
Nov 22 at 10:21




Would you have a ScreenPrint of the ViewPage you created?
– Charles de M.
Nov 22 at 10:21












Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
– Charles de M.
Nov 22 at 11:27




Solved!: I had a Binding mistake in My Post Method. Sorry for that. Next time I will check my code even more thorough before posting a question. Thank you for your help and support!
– Charles de M.
Nov 22 at 11:27


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376693%2fwhen-creating-double-dropdown-selectlist-with-jquery-on-post-returns-0-asp-net%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin