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.
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
add a comment |
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.
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
the ClientUser is set to 0, instead of the choice
I think it wasClientUserList
instead ofClientUser
– Foo
Nov 19 at 14:38
add a comment |
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.
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
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.
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
javascript jquery asp.net asp.net-core
asked Nov 19 at 14:25
Charles de M.
479
479
the ClientUser is set to 0, instead of the choice
I think it wasClientUserList
instead ofClientUser
– Foo
Nov 19 at 14:38
add a comment |
the ClientUser is set to 0, instead of the choice
I think it wasClientUserList
instead ofClientUser
– 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
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I can't reproduce your issue :
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 :
- 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 .
- Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .
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 howasp-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 haveViewBag.countries = new SelectList(_context.Customer, "Id", "Name");
and in the View useasp-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
add a comment |
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 :
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 :
- 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 .
- Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .
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 howasp-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 haveViewBag.countries = new SelectList(_context.Customer, "Id", "Name");
and in the View useasp-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
add a comment |
up vote
0
down vote
I can't reproduce your issue :
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 :
- 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 .
- Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .
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 howasp-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 haveViewBag.countries = new SelectList(_context.Customer, "Id", "Name");
and in the View useasp-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
add a comment |
up vote
0
down vote
up vote
0
down vote
I can't reproduce your issue :
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 :
- 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 .
- Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .
I can't reproduce your issue :
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 :
- 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 .
- Use Jquery to get the selected opinion , use Ajax to call action function and pass the value as parameter .
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 howasp-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 haveViewBag.countries = new SelectList(_context.Customer, "Id", "Name");
and in the View useasp-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
add a comment |
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 howasp-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 haveViewBag.countries = new SelectList(_context.Customer, "Id", "Name");
and in the View useasp-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
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%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
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
the ClientUser is set to 0, instead of the choice
I think it wasClientUserList
instead ofClientUser
– Foo
Nov 19 at 14:38